【发布时间】:2013-04-17 20:46:42
【问题描述】:
destinationDate 和组件的结果不同。 我该如何解决这个问题?
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSDate* sourceDate = [NSDate date];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit|
NSSecondCalendarUnit) fromDate:destinationDate];
NSLog(@"%@", destinationDate);
NSLog(@"%@", components);
2013-04-24 19:27:09 +0000
历年:2013 月:4 闰月:无 天数:25 小时:4 分钟:27 第二:9
【问题讨论】:
-
您的问题是什么,日期描述将始终以 GMT 表示。您的 localTimeZone 似乎比格林威治标准时间晚 5 小时。第一个日志可以解释为下午 4:27,它与组件中显示的相同
-
你有答案吗?
-
我正在努力解决一个很接近的问题,但我无法从您的代码中理解您最初想要实现的目标 - 您能否用几句话来说明这一点?
标签: ios objective-c nsdatecomponents