2012-08-30 64 views
3

如果我發送方法dateByAddingTimeInterval:到NSDate的,象下面這樣:dateByAddingTimeInterval參數語法

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

NSDate按秒計算時間。

+0

啊我笨了......謝謝! – eulr