Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot convert this datetime string from Django to iOS NSDate

I am getting NSString 2012-08-17T10:56:45.508205 as time from Django API.

I am trying to convert that string into NSDate object with this code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:stringToConvert];

But date object is null. Where am I going wrong?

like image 803
cagatay celebioglu Avatar asked Oct 26 '25 14:10

cagatay celebioglu


1 Answers

The ISO8601 string you're getting from the Django API (2012-08-17T10:56:45.508205) does not include a time zone component. However, you are including that field symbol (Z) in your format string. Just remove that and it should work fine.

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

See the Unicode Technical Reference for more information on the formatting codes:

like image 100
Brad Eaton Avatar answered Oct 28 '25 08:10

Brad Eaton