Ashish Agarwal
Ashish Agarwal

Reputation: 14925

Converting UTC string into NSDate

I have a date-time string returned by the server in UTC format: 2015-04-21T00:54:46.469Z

I am trying to convert this into NSDate. The code is:

NSString *dateString = @"2015-04-21T00:54:46.469Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-DDThh:mm:ss.sZ"];
NSDate *locationDate = [dateFormatter dateFromString:dateString];

The value of locationDate is nil after executing this

Upvotes: 3

Views: 1707

Answers (1)

chenzhongpu
chenzhongpu

Reputation: 6871

You should use

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];

or

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];

Any way, T must be in quotes. It seems to be a bug in Apple.

Upvotes: 5

Related Questions