Search code examples
iosobjective-cnsdatensdateformatter

NSDateFormatter returns nil for date string from server


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?


Solution

  • There was no 2:40 in the morning on the 31st of March in Paris - Daylight savings/summer time started at 02:00 on the 31st of March, meaning the clocks jumped forward to 03:00 at that time.

    nil is the correct result