dateByAddingTimeInterval参数语法

时间:2012-08-30 02:17:20

标签: objective-c oop nsdate

如果我将方法dateByAddingTimeInterval:发送到NSDate,如下所示:

NSDate *today = [NSDate date]; 
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0];

NSArray *dates = [NSArray arrayWithObjects: today, tomorrow, yesterday, nil];

NSLog(@"today's date is %@", [dates objectAtIndex:0]);
NSLog(@"yesterday's date was %@", [dates objectAtIndex:2]);

我得到了这个输出:

...The first date is 2012-08-30 02:14:19 +0000
...The third date is 2012-08-30 02:13:55 +0000

这很奇怪,因为第三次约会应该是2012-08-29

但是......如果我将NSDate消息更改为:

NSDate *today = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

为什么要添加* 60.0 ...

...The first date is 2012-08-30 02:15:25 +0000
...The third date is 2012-08-29 02:15:25 +0000

使输出正确吗?

谢谢。

1 个答案:

答案 0 :(得分:4)

NSDate按秒计算时间。