【发布时间】:2014-06-26 18:01:45
【问题描述】:
我正在尝试计算从一个日期到另一个日期的天数,并找到了两种方法。
第一个使用NSDateComponents如下:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:date1
toDate:date2
options:0];
然后检查[组件日]。
第二个使用timeInterval:
NSInteger timeInterval = [date2 timeIntervalSinceDate:date1];
然后得到 timeInterval/3600/24
一种方法比另一种更好吗(可能是因为 timeInterval 方法缺乏精度)?
谢谢!
【问题讨论】:
标签: ios objective-c nsdate nstimeinterval