【发布时间】:2016-11-21 20:59:23
【问题描述】:
我从服务器获取时间是2016/07/19--12:20:00
现在我必须根据国家/地区更改这个时间。
例如,如果我在奥克兰,那么时间必须喜欢2016/07/19--18:50:00
我在堆栈溢出中找到了很多解决方案,但我无法解决这个问题。
请帮帮我
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd--HH:mm:ss"];
NSDate* sourceDate = [dateFormatter dateFromString:model.tripEndTime];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:@"YYYY/MM/dd--HH:mm:ss"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(@"localTime:%@", localTime);
我用这段代码试过
【问题讨论】:
-
你能展示你尝试过的代码吗
-
服务器的时间是GMT吗?
标签: ios objective-c timezone nsdate