简体   繁体   English

NSDate在模拟器和设备(ipad)方面有所不同

[英]NSDate different in simulator and device(ipad)

Hi I am converting a GMT time to local time in my project - I getting the correct value in all simulator and in iPhone/iPod - but when I run this in iPad I am getting null, Here is my code - 嗨,我正在将我的项目中的GMT时间转换为本地时间-在所有模拟器和iPhone / iPod中都获得了正确的值-但是在iPad上运行时,我得到的是空值,这是我的代码-

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)

Issue is with user's ipad's time format - if it is in 12 hour format - then the above code results null What do I do for this issue ?? 问题是用户的ipad的时间格式-如果是12小时格式-则以上代码结果为空我该怎么办?

bacause you time format is set to 12 hours on ipad device, the date string is in 24 hours format look at string @"14:37:00". 因为您在ipad设备上将时间格式设置为12小时,所以日期字符串为24小时格式,请查看字符串@“ 14:37:00”。

use 采用

dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

Reference 参考

if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX. 如果要强制将其设置为12小时制,而无论用户的24/12小时制设置如何,都应将语言环境设置为en_US_POSIX。

Use hh instead of HH. 使用hh代替HH。

hh usually is 12 hour time while HH is usually 24 hour time. hh通常是12小时,而HH通常是24小时。

A good official reference for this, as linked by Apple themselves , is here . 由Apple自己链接的一个很好的官方参考书在这里 Another good table is here , as mentioned by Zaph. 正如Zaph所提到的,另一个好的表在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM