Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 723
Ashish Agarwal Avatar asked Jun 01 '26 11:06

Ashish Agarwal


1 Answers

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.

like image 82
chenzhongpu Avatar answered Jun 03 '26 01:06

chenzhongpu