我想转换从网络服务获得的GMT时间。我得到像这样的对象
dateTime: {
date: 23
day: 3
hours: 6
minutes: 13
month: 8
nanos: 0
seconds: 0
time: 1442988780000
timezoneOffset: 0
year: 115
}
我不知道如何将此对象转换为本地手机时间。
答案 0 :(得分:2)
您可以使用NSDateComponents和NSDate。请参阅Apple的Date and Time Programming Guide
答案 1 :(得分:1)
Step1-
转换您在Dictionary or Array
中获得的数据
Step2-
按key-value
对访问字典。
'Step3-'参考Apple doc的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);