2013-05-10 70 views
-2

我現在的設備時間--- 2013年5月9日上午10:23問題在coverting GMT時間當地時間

我把它轉換詮釋GMT與下面的代碼

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a"; 

    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [dateFormatter setTimeZone:gmt]; 
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"time in GMT ------ %@",timeStamp); 

而且我得到了出來把儘可能

 time in GMT ------ 2013-05-10 04:53 AM 

然後我發現我的時區

NSTimeZone *localTime = [NSTimeZone systemTimeZone]; 
    NSLog(@"local timezone is------ %@",[localTime name]); 

出把

local timezone is------ Asia/Kolkata 

現在我需要上述GMT時間轉換爲本地再寄一次用下面的代碼

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:[localTime name]]; 
    [dateFormatter setTimeZone:sourceTimeZone]; 
    NSDate *dateFromString = [dateFormatter dateFromString:timeStamp]; 
    NSLog(@"local time is ------- %@",dateFromString); 

但我得到一個錯誤的出放..

local time is ------- 2013-05-09 19:23:00 +0000 

爲什麼會發生這種情況? 我應該得到本地時間 - 2013年5月9日上午10:23

回答

0

//獲取當前日期/時間

NSDate *today = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
NSString *currentTime = [dateFormatter stringFromDate:today]; 
[dateFormatter release]; 
NSLog(@"User's current time in their preference format:%@",currentTime); 
+0

這些都沒有解決的問題。這並不能解釋爲什麼最終輸出顯示錯誤的時間。 – rmaddy 2013-05-10 05:12:40

1

timestamp字符串在GMT。這意味着當你想將GMT字符串轉換回NSDate時,你的日期格式化器需要設置爲GMT,而不是你的本地時區。通過這樣做,您將得到正確的NSDate

不要忘記,當你登錄一個NSDate對象時,它將以GMT顯示,不管其他什麼。

0
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a"; 

    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [dateFormatter setTimeZone:gmt]; 
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"time in GMT ------ %@",timeStamp); 



    NSTimeZone *localTime = [NSTimeZone systemTimeZone]; 
    NSLog(@"local timezone is------ %@",[localTime name]); 


    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [dateFormatter setTimeZone:sourceTimeZone]; 
    NSDate *dateFromString = [dateFormatter dateFromString:timeStamp]; 
    NSLog(@"local time is ------- %@",dateFromString); 


    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
    dateFormatter1.dateFormat = @"yyyy-MM-dd HH:mm"; 

    NSTimeZone *gmt1 = [NSTimeZone localTimeZone]; 
    [dateFormatter1 setTimeZone:gmt1]; 
    NSString *timeStamp1 = [dateFormatter1 stringFromDate:[NSDate date]]; 
    NSLog(@"local time is ------- %@",timeStamp1); 

輸出:

time in GMT ------ 2013-05-10 05:13 AM 
local timezone is------ Asia/Kolkata 
local time is ------- 2013-05-10 00:13:00 +0000 
local time is ------- 2013-05-10 10:43 
+0

這不回答問題。目標是將'timestamp'正確地轉換回'NSDate'。 – rmaddy 2013-05-10 05:19:10

0

嘗試使用此代碼:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"]; 

    // get time zone 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
    NSDate *date = [NSDate date]; 
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
    date = [dateFormatter dateFromString:timeStamp]; 

    // change time zone 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Kolkata"]]; 
    // or you can use SystemTimeZone 
    // [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 

    NSString *strDateWithLocalTimeZone = [dateFormatter stringFromDate:date]; 

    NSLog(@"Local Time Zone Date %@", strDateWithLocalTimeZone);