Date formatting in iOS 9 does not work

I have a problem with date formatting in iOS 9, it works fine with iOS 8, and also works when I test it in a simulator with iOS 9, but when the test on a real device with iOS 9, I get null. Below is the code I'm using

NSLog(@"String Date: '%@'", stringDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:stringDate];
NSLog(@"Date: %@", date);

In the log I get:

String Date: '8/29/2015 4:13:39 PM'
Date: (null)

Also, if I use uppercase h (H or HH), I always get that hour is 10.

+4
source share
1 answer

Try setting the locale in your date formatting.

[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

From the documentation for Apple :

, locale - . en_US_POSIX, a , .

EDIT:

dateFormat.locale = NSLocale(localeIdentifier: "en_US_POSIX")
+5

Source: https://habr.com/ru/post/1610156/


All Articles