您好,我在我的项目中将 GMT 时间转换为本地时间 - 我在所有模拟器和 iPhone/iPod 中都得到了正确的值 - 但是当我在 iPad 中运行它时,我得到的是 null, 这是我的代码-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *startDate = [dateFormatter dateFromString:@"14:37:00"];
NSLog(@"startDate---%@",startDate);
//This is startDate---2000-01-01 14:37:00 +0000 (in simulator/iPod/iphone)
// startDate---(null) (in ipad)
问题出在用户 ipad 的时间格式上——如果它是 12 小时格式——那么上面的代码结果为 null 我该怎么办??
最佳答案
因为你的时间格式在 ipad 设备上设置为 12 小时,日期字符串是 24 小时格式看字符串@"14:37:00"。
使用
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
如果你想强制它进入 12 小时模式,无论用户的 24/12 小时模式设置如何,你都应该将语言环境设置为 en_US_POSIX。
使用 hh 而不是 HH。
hh 通常是 12 小时制,而 HH 通常是 24 小时制。
一个很好的官方引用,as linked by Apple themselves , 是 here .另一个好表是 here ,如 Zaph 所述。
关于ios - NSDate 在模拟器和设备(ipad)中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364542/