-2

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

我使用以下代码将其转换为 int 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-05-09 10:23 AM

4

5 回答 5

1

你的timestamp字符串是格林威治标准时间。这意味着当您要将 GMT 字符串转换回 时NSDate,您的日期格式化程序需要设置为 GMT,而不是您的本地时区。通过这样做,您将获得正确的NSDate.

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

于 2013-05-10T05:10:30.017 回答
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);
于 2013-05-10T05:07:48.640 回答
0

请亲爱的在谷歌上做一点努力意味着首先在谷歌上搜索你的问题,如果在这种情况下你没有得到你的答案,请将你的问题放在 stackover flow 上。并在下面的链接中查看您的答案....

  1. iPhone:NSDate 将 GMT 转换为本地时间
  2. iphone时间转换gmt到本地时间
  3. 日期/时间转换为用户本地时间 - 问题
于 2013-05-10T05:09:17.507 回答
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
于 2013-05-10T05:13:14.180 回答
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);
于 2013-05-10T05:22:00.560 回答