timeIntervalSinceDate在过去?

时间:2012-11-12 20:59:00

标签: objective-c ios

我在过去创建一个NSDate(过去1小时)并且看起来设置正确,但我只想使用它来确定偶数是否已经发生。因为我设置过去,当我做检查时,它肯定会认为偶数已经发生了,但是timeIntervalSinceDate似乎只能给出一个积极的结果?

这是我用timeIntervalSinceDate

的代码
 NSDate *now = [NSDate date];
  NSTimeInterval secondsSincePlantingInterval = [now timeIntervalSinceDate:plantingDate];

这是68秒,但它应该是-68秒,还是不返回负值?

2 个答案:

答案 0 :(得分:5)

这是完全正常的记录行为

documentation州:

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
  

返回值

     

接收者和另一个日期之间的间隔。如果接收者早于anotherDate,则返回值为负。

由于plantingDate是过去的,因此接收方早于。因此价值是正面的。

而且;这是简单的英语

 [now timeIntervalSinceDate:plantingDate];

因此,种植日期到现在的时间间隔为正。

答案 1 :(得分:3)

它确实返回负值,是的。但如果plantingDate过去,那么我希望secondsSincePlantingInterval为正。

如果您考虑一下,那条线就是:

  

告诉我 now以来plantingDate 的秒数。

就像周二第2天是自星期一1日起的一天,nowplantingDate后的+秒。

你想:

NSTimeInterval secondsSincePlantingInterval = [plantingDate timeIntervalSinceNow];