问题
This seems like an iOS 10 bug. It is OK in iOS 8 and 9.
The hour description is wrong for [NSDate date].description. It is appending the 24 hour description and 12 hour description. I am not using NSDateFormatter. Just the default settings.
On a real iOS 10 device, in Settings:
- Set Region to Japan in Settings > General > Language & Region > Region Formats > Region
- Set 24-Hour Time Off in Settings > General > Date & Time > 24-Hour Time
Then with:
NSLog(@"Date is %@", [NSDate date].description);
Actual log output:
Date is 2016-11-14 44:14:57 AM +0000
Expected output:
Date is 2016-11-14 4:14:57 AM +0000
Edit (Adding another example):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss a";
NSString *dateString = @"1985-04-12 7:20:50 PM";
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"Date is %@", date.description);
Actual output:
Date is 1985-04-12 1010:20:50 AM +0000
Expected output
Date is 1985-04-12 10:20:50 AM +0000
If I change the hour to 24 hour format (HH):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss a";
NSString *dateString = @"1985-04-12 7:20:50 PM";
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"Date is %@", date.description);
Actual output:
Date is (null)
Expected output:
Date is 1985-04-12 10:20:50 AM +0000
回答1:
We were also facing similar issue for iOS 10.x and fixed it using
NSLocale *POSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; dateFormatter.locale should equal(POSIXLocale);
来源:https://stackoverflow.com/questions/40581850/ios-10-bug-nsdate-hour-description-with-japan-region-and-24-hour-time-off