Reputation: 10959
I have one Date in string format "2013-03-19T19:00:50
"
I am trying to convert it into NSDate
using NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-ddThh:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(@"date in date format : %@",startDate);
but it is giving me null date
date in date format : (null)
What is the issue?
Upvotes: 0
Views: 1276
Reputation: 5955
Do like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(@"date in date format : %@",startDate);
Upvotes: 0
Reputation: 90127
According to the date formatter patterns hh
means Hour [1-12]
You want Hour [0-23]
which is HH
.
And any letters that are not date format patterns, or must not be interpreted in this way have to be put in between apostrophes.
use [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
Upvotes: 1
Reputation: 46563
Use :
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
You have time in 24 hour format, so HH
is required. hh
is used when time is in 12 hour format.
And T
is required to be in single quote, T is not a part of date this is an added text on it.
Upvotes: 7