[dateFormat dateFromString:dateStr] is giving nil

NSDateFormatter is giving different values in different devices.

Code:

  • (NSString *)GetBuildDate {

    NSString *buildDate;     // Get build date and time, format to 'yyMMddHHmm'     NSString *dateStr = [NSString stringWithFormat:@"%@", [NSString stringWithUTF8String:DATE]];

    // Convert to date     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];     [dateFormat setDateFormat:@"MMM dd yyyy"];     [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];     NSDate *date = [dateFormat dateFromString:dateStr];

       // Set output format and convert to string     [dateFormat setDateFormat:@"dd MMM yyyy"];     buildDate = [dateFormat stringFromDate:date];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    [df setDateFormat:@"dd"];

    NSString *myDayString = [df stringFromDate:date];     [df setDateFormat:@"MM"];     NSString *myMonthString = [df stringFromDate:date];     NSString *myMonth  = [NSString stringWithFormat:@"%@ %@",myDayString,[[df monthSymbols] objectAtIndex:([myMonthString intValue]-1)]] ;     [df setDateFormat:@"yyyy"];

    return [NSString stringWithFormat:@"%@ %@",myMonth,[df stringFromDate:date]];

}

Device having issue: On iPhone XR, iOS v15.6.1 , when iPhone language is selected English, its show the date correctly. Whereas when user changes the iPhone language in settings to Chinese(specifically) dateFromString returns nil. Which is later crashing for [[df monthSymbols] objectAtIndex:([myMonthString intValue]-1)].

In most of the other devices there is no such issue with the NSDateFormatter having same iPhone settings.

Would like to get more clarity why there is difference on different devices. How changing the language setting changing value for the date.

Answered by DTS Engineer in 728918022

If you’re working with fixed-format strings, you must pin the locale to en_US_POSIX. See QA1480 NSDateFormatter and Internet Dates for all the details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

If you’re working with fixed-format strings, you must pin the locale to en_US_POSIX. See QA1480 NSDateFormatter and Internet Dates for all the details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for the reply. @eskimo Setting locale works for me. But question is still why it is working for some devices and and getting nil for some. Is there any setting in the iPhone which causing this issue?

But question is still why it is working for some devices and and getting nil for some.

Because of if you don’t force the locale to something specific it uses the user’s current locale. That means it might end up trying to parse fixed-format English dates as if they were Chinese. Or 12-hour time as if it were 24-hour time. And so on. There are a bazillion things that can go wrong.

My particular least favourite example is in QA1480, where folks who happen to use the Buddhist calendar get results back but they are off by about 500 years.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[dateFormat dateFromString:dateStr] is giving nil
 
 
Q