将GMT时间转换为当地时间的问题

时间:2013-05-10 05:03:30

标签: iphone ios nsdateformatter gmt nstimezone

我目前的设备时间是--- 2013-05-09 10:23 AM

我将其转换为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]);

out put

  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-05-09 10:23 AM

5 个答案:

答案 0 :(得分:1)

您的timestamp字符串位于GMT中。这意味着当您想要将GMT字符串转换回NSDate时,您的日期格式化程序需要设置为GMT,而不是当地时区。通过这样做,您将获得正确的NSDate

不要忘记,当您记录NSDate对象时,它将以GMT显示,而不管其他任何内容。

答案 1 :(得分: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);

答案 2 :(得分:0)

请亲爱的,谷歌的努力意味着首先在谷歌搜索你的问题,如果在这种情况下你没有得到你的答案,请将你的问题放在堆栈流上。 并在下面的链接上看到你的答案......

  1. iPhone: NSDate convert GMT to local time
  2. iphone time conversion gmt to local time
  3. Date/time conversion to user's local time - issue

答案 3 :(得分: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

答案 4 :(得分: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);