【发布时间】:2015-03-28 03:40:59
【问题描述】:
如何计算从现在到特定日期和时间(例如下个月 20 日下午 6 点)的时间差(以秒为单位)?
我尝试了很多,但没有经过大量计算和检查很多不同的案例,就没有成功地找到一个简单的解决方案。
NSDate *now = [NSDate date];
NSDateComponents *dc = [[NSDateComponents alloc] init];
[dc setMonth:1];
MyTargetDateObject = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:now options:0];
[dc release];
..从现在开始增加 1 个月。其他例子例如here 似乎不适用。
【问题讨论】:
-
日期是秒。所以形成两个日期并减去。
标签: objective-c cocoa-touch nsdate nstimeinterval date-arithmetic