Reputation: 127
I'm trying to parse a date passed in the format:
"2014-03-26T05:07:42.14286Z"
My NSDateFormatter
code looks like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
self.createdAt = [dateFormatter dateFromString:@""2014-03-26T05:07:42.14286Z""];
But it just returns nil
. I've tried multiple variations with and without the ticks but I seem to be missing something. Am I using NSDateFormatter
incorrectly, misunderstanding the usage of ticks or something else entirely?
Upvotes: 1
Views: 2033
Reputation: 15005
Just replace the below line to modified line
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
Modified line:-
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Upvotes: 0
Reputation: 11439
The formatter returns nil if the given string doesn't correspond to the expected format. Your format string was almost right, you just needed to :
The correct format string is :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *d = [df dateFromString:@"2014-03-26T05:07:42.14286Z"];
Upvotes: 6