2011-10-26 51 views
1

在我的iPhone应用程序,我莫名其妙地需要的时间价值,并把它想: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时间?

回答

1

你可能要考虑的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到小时值不良好的做法。夏令时如何处理?或者当另一个时区中的某个人使用您的应用程序?

使用NSDateFormatter可能是您的最佳解决方案。

+0

谢谢,会试试看。 – kubilay

相关问题