在我的 iPhone 应用程序中,我以某种方式需要时间值并像这样获取它:
//Get the full time here
NSDate *now = [NSDate date];
//Seperate as time and date
NSArray *arr = [[[NSString alloc] initWithFormat:@"%@",now] componentsSeparatedByString:@" "];
//Get the time value
NSString *timeOnly = [arr objectAtIndex:1];
//Seperate it like hour, minute and second values
NSArray *timeOnlyArray = [timeOnly componentsSeparatedByString:@":"];
NSInteger hour = [[timeOnlyArray objectAtIndex:0] intValue];
NSInteger minute = [[timeOnlyArray objectAtIndex:1] intValue];
除一件事外一切正常:即使我的计算机和模拟器的时间设置为本地时间 (GMT +2),我在模拟器上调试时获得的值是 GMT 时间(比本地时间早 2 小时)。
没关系,我可以简单地将小时值加 2,但我不知道它在设备上是否相同。如果我以这种方式发布它,它会在设备上工作吗?或者我应该添加 2 小时,因为应用程序也会在真实设备中查看 GMT 时间?
最佳答案
您可能想查看 NSDateFormatter 类(如 AppleDocs 中所写:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) 完成后,请阅读此处:Convert UTC NSDate to local Timezone Objective-C
仅将 2 添加到 hours-value 不是好的做法。您将如何处理夏令时?或者其他时区的人何时会使用您的应用?
使用 NSDateFormatter 可能是最适合您的解决方案。
关于ios - iPhone模拟器时差是否保留在设备上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900080/