iOS 10 bug? NSDate hour description with Japan region and 24-Hour Time off

混江龙づ霸主 提交于 2019-12-23 19:10:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!