I am using following method to convert NSDate to NSString
- (void)splitDateintoDateTime:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
strDate = [formatter stringFromDate:date];
[formatter setDateFormat:#"hh:mm a"];
strTime = [formatter stringFromDate:date];
}
The format of date is yyyy-MM-dd hh:mm:ss z
I want separate NSDate values for Date and Time with format mentioned in the method.
- (void)splitDateintoDateTime:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"dd/MM/yyyy";
NSString *dateString = [dateFormatter stringFromDate: date];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm z";
NSString *dateString = [timeFormatter stringFromDate: date];
}
Related
I search some stack quentions for converting string to date and that's working fine, but i convert my string date to NSDate than it's return me nil. Here is my code :
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
I want to convert this date to yyyy-MM-ddTHH:mm:ss.SSSZ this formate.
What's problem here ?
do like
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// set the date format related to what the string already you have
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
// again add the date format what the output u need
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *finalDate = [dateFormat stringFromDate:date];
NSLog(#"finalDate==%#",finalDate);
output
Your problem is that your date format #"yyyy-MM-ddTHH:mm:ss.SSSZ" doesn't match your date string #"2016-07-29 09:05". You need to use a different date format.
Working copy -
Objective C
NSString *dateStr = #"2016-07-29T09:05:00.00000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
Swift 2.x
var dateformater = NSDateFormatter()
dateformater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
var dateString:String = "2016-07-29T09:05:00.00000Z"
let date:NSDate = dateformater.dateFromString(dateString)!
print(date)
Try this code.
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"the date is %#",date);
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString *currentDate = [dateFormatter stringFromDate:date];
NSDate *finaldate = [dateFormatter dateFromString:currentDate];
NSLog(#"CurrentDate:%#", finaldate);
Just change your formatterstyle as below
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
After that you can convert the date into below format :
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSString *str = [dateFormat stringFromDate:date];
NSLog(#"%#",str);
What is the date and time format for "06/08/2016 09:50:12" to be converted to "dd MMMM YYYY HH:mm"?, which i'm converting the NSDate to NSString.
Use this code,
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSString *result = [dateFormat stringFromDate:date];
its working for me, hope its helpful.
Try this code:
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
// string to date
NSString *dateStr = #"06/08/2016 01:50:12";
NSDate *date = [format dateFromString:dateStr];
// date to string
[format setDateFormat:#"dd MMMM yyyy HH:mm"];
NSString *dateStr2 = [format stringFromDate:date];
NSLog(#"%#", dateStr2);
Try this,
NSString *strDate = #"06/08/2016 09:50:12";
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:s"];
NSDate *date = [format dateFromString:strDate];
NSLog(#"%#",date);
To convert this date into string :
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:s"];
NSString *str = [format stringFromDate:date];
Hope this help
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:ss"];
// string to date
NSString *dateStr = #"06/08/2016 01:50:12";
NSDate *date = [format dateFromString:dateStr];
NSLog(#"%#", date);
// date to string
NSDate *date2 = [NSDate date];
NSString *dateStr2 = [format stringFromDate:date2];
NSLog(#"%#", dateStr2);
//#"dd/MM/YYYY HH:mm:ss"
//dd is for the day.
//MM is for the month.
//YYYY is for the year.
//HH is for the hour.
//mm is for the minute.
//ss is for the second.
Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me
I am converting this date string "05/20/2015 05:27 pm" into NSDate with the help of formatter but it returns nil.
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDate *date = nil;
[self.dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
if (strDate) {
if (strDateFormat) {
[self.dateFormatter setDateFormat:strDateFormat];
}
date = [self.dateFormatter dateFromString:strDate];
}
return date;
}
I am using the above function where I am passing the string as "05/20/2015 05:27 pm" with format #"dd/MM/yyyy HH:mm:ss".
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *stringDate = [dateFormatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", stringDate);
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strDateFormat];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormatter dateFromString:strDate];
return date;
}
NSString *dateStr = #"05/20/2015 05:27 pm";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
Nslog(#"%#",date);
To transform from NSDate to timestamp you can use:
NSDate *date = [NSDate date];
NSTimeInterval timestampSeconds = [date timeIntervalSince1970];
Result will be in seconds. To transform timestamp to standard milliseconds just multiply by 1000.
timeIntervalSince1970
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
NSDate *strDate = [formatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", strDate);//NSDate
time_t timeStamp = (time_t) [strDate timeIntervalSince1970];
NSLog(#"%ld", timeStamp);//timestamp
How to convert NSDate into unix timestamp iphone sdk?
This might helps you :)
I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);