将 GMT 时间对象转换为当地时间 iOS

convert GMT time object to local time iOS

我想转换从网络服务获取的 GMT 时间。我得到这样的对象

dateTime: {
           date: 23
           day: 3
           hours: 6
           minutes: 13
           month: 8
           nanos: 0
           seconds: 0
           time: 1442988780000
           timezoneOffset: 0
           year: 115
          }

我不知道如何将此对象转换为本地 phone 时间。

您可以使用NSDateComponents and NSDate. See Apple's Date and Time Programming Guide

Step1- 转换您在 Dictionary or Array 中获得的数据 Step2- 通过 key-value 对访问字典。 'Step3-' 参考 NSDate 来自苹果文档

也试试这个-

NSString *dateStr = @"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(@"DateString : %@", dateStr);