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.
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"