NSDateFormatter returns nil for date string from server

  Kiến thức lập trình

I received a date string and a timezone from a web service and I am needing to convert into an NSDate. When setting the format and timezone, I get nil:

NSString *timeZoneString = @"Europe/Paris";
NSString *dateString = @"2024-03-31T02:40:21.000";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SS"];

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneString];
[dateFormat setTimeZone:timeZone];

NSDate *date = [dateFormat dateFromString:dateString];// return nil

Is there a reason why this specific date string, and using Europe/Paris returns nil when trying to create my date?

LEAVE A COMMENT