2011-08-17 24 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)引用以來的時間,它沒有其他概念。

相關問題