3

如果我将方法 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

使输出正确?

谢谢你。

4

1 回答 1

4

NSDate 按秒计算时间。

于 2012-08-30T02:21:25.760 回答