2014-10-02 51 views
-1

嗨我想顯示GMT時間到我的設備時間。 我的設備時區是EDT。NSDate顯示錯誤的日期與NSTimezone

我得到了GMT時間作爲字符串

 // GMT Time-----2014-11-27 19:32:00 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"HH:mm:ss"; 
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSDate *startDate = [dateFormatter dateFromString:@"2014-11-27 19:32:00"]; 

我得到這個開始日期 -

//startDate---2014-11-27 19:32:00 +0000 

現在我想將其轉換成我的本地時間

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init]; 
dateFormatter2.dateFormat = @"hh:mm a"; 
NSTimeZone *local = [NSTimeZone localTimeZone]; 
[dateFormatter2 setTimeZone:[NSTimeZone localTimeZone]]; 
NSString *startDateStr = [dateFormatter2 stringFromDate:startDate]; 

根據我的時區(EDT),結果必須是 - 3:32 PM 但是我錯誤的時間是

//startDateStr---02:32 PM 

我有我的打印與GMT

NSLog(@"TimeZone %d",[[NSTimeZone localTimeZone] secondsFromGMT]); 
    // TimeZone -14400  - this correct and as per this the startDate must be 03:32 PM 

時區的區別在哪裏,我做了錯誤? 請幫我

回答

0

非常聰明。您的轉換日期爲2000年1月1日。因此,夏令時間有所不同。格林威治標準時間19:32在您的時區下午2點32分。但是今天19:32 GMT是你所在時區的3:32 pm。

+0

請參閱我更新的問題。我已經改變了開始日期,但仍然錯誤的時間。 – 2014-10-02 12:08:32