我在过去创建一个NSDate(过去1小时)并且看起来设置正确,但我只想使用它来确定偶数是否已经发生。因为我设置过去,当我做检查时,它肯定会认为偶数已经发生了,但是timeIntervalSinceDate似乎只能给出一个积极的结果?
这是我用timeIntervalSinceDate
的代码 NSDate *now = [NSDate date];
NSTimeInterval secondsSincePlantingInterval = [now timeIntervalSinceDate:plantingDate];
这是68秒,但它应该是-68秒,还是不返回负值?
答案 0 :(得分:5)
这是完全正常的记录行为
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
返回值
接收者和另一个日期之间的间隔。如果接收者早于anotherDate,则返回值为负。
由于plantingDate
是过去的,因此接收方早于不。因此价值是正面的。
而且;这是简单的英语
[now timeIntervalSinceDate:plantingDate];
因此,种植日期到现在的时间间隔为正。
答案 1 :(得分:3)
它确实返回负值,是的。但如果plantingDate
过去,那么我希望secondsSincePlantingInterval
为正。
如果您考虑一下,那条线就是:
告诉我
now
以来plantingDate
的秒数。
就像周二第2天是自星期一1日起的一天,now
是plantingDate
后的+秒。
你想:
NSTimeInterval secondsSincePlantingInterval = [plantingDate timeIntervalSinceNow];