2011-08-17 35 views
6

我想获得当前的日期与添加设备时区,但显示原始日期晚了1小时。我的事情,我遇到了DaylightSavingTime的问题。 如何禁用isDaylightSavingTime = FALSE。如何获取当前NSDate相同的模拟器日期? iPhone的Sdk

这里是代码,我用..

NSDate *date = [NSDate Date]; 
NSTimeZone *currentTimeZon = [NSTimeZone localTimeZone]; 
if ([currentTimeZon isDaylightSavingTime]) 
{ 
    NSLog(@"East coast is NOT on DT"); 
} 
else 
{ 
    NSLog(@"East coast is on DT"); 

} 
NSTimeInterval timeZoneOffset = [currentTimeZon secondsFromGMTForDate:currentDate]; 
NSTimeInterval gmtTimeInterval = [date timeIntervalSinceReferenceDate] - timeZoneOffset; 
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval]; 
NSLog(@"### Announcement gmtDate = %@",gmtDate); 

我得到的时间为1小时的时差,日期是perfact。

+1

每个时刻只有一个NSDate(不管你在哪里)。如果您想在不同的时区打印日期,请使用NSDateFormatter。 – 2011-08-17 11:58:41

回答

1

使用NSCalendar,它理解时区,日光节约等。正如@albertamg所说,NSDate只是自UTC(GMT)引用以来的时间,它没有其他概念。