【发布时间】:2020-05-10 23:25:43
【问题描述】:
我只是没有得到正确的结果......所以根据时区我想要两个日期之间的日历日差。因此,如果一个从第 1 天的 23:00 开始并在第 2 天的 14:00 结束,它应该返回 1。现在我的方法返回 0,为什么?因为不到24小时?示例:
我的日志:
CheckForPictures departure date: Tue Jan 28 23:10:00 2020 destinationDate: Wed Jan 29 09:30:00 2020 in timeZone:Europe/Zurich and get a day difference: 0
(电脑也有苏黎世时区,所以是当地时间)
我的方法:
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:timeZone];
NSDateComponents *components = [calendar components:NSCalendarUnitDay
fromDate:self.departureTime
toDate:self.destinationTime
options:0];
NSLog(@"CheckForPictures departure date: %@ destinationDate: %@ in timeZone:%@ and get a day difference: %ld", self.departureTime, self.destinationTime, timeZone.name, components.day);
return components.day;
并且这段代码返回 0 并在 log 以上记录...
【问题讨论】:
标签: objective-c nsdate nsdatecomponents