Reputation: 171
If I send the method dateByAddingTimeInterval: to NSDate, like below:
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]);
I get this output:
...The first date is 2012-08-30 02:14:19 +0000
...The third date is 2012-08-30 02:13:55 +0000
Which is strange because the third date should have been 2012-08-29
But... if I change the NSDate messages to:
NSDate *today = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
Why does adding the * 60.0...
...The first date is 2012-08-30 02:15:25 +0000
...The third date is 2012-08-29 02:15:25 +0000
make the output correct?
Thank you.
Upvotes: 3
Views: 2404