destinationDate和组件的结果不同。 我该如何解决这个问题?NSDate转换NSDateComponents问题
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年4月24日19时27分09秒+0000
日历年份:2013 月:4 闰月:没有 日:25 小时:4 分:27 第二:9
你是什么问题就在这里,日期的说明会始终处于GMT。它看起来像是你的localTimeZone比GMT落后5个小时。第一个日志可以解释为4:27 PM,它与组件 – Anupdas 2013-04-24 10:38:24
中显示的相同,你是否已经得到了答案。 – Balu 2013-04-24 11:19:52