This is an interesting question, and I worked a lot on the solution. These are my findings:
NSLog(@"CurrentDate: %@", [NSDate date]);
The code shown above will have the same result as the code shown below:
NSLog(@"CurrentDate: %@", [[NSDate date] description]);
Reading with NSDate Class Reference gives this documentation using the NSDate description method.
Constant performance in different versions of the operating system is not guaranteed. To format a date, use the date formatting object instead (see NSDateFormatter Guide and Data Formatting Guide).
I also looked at the documentation for descriptionWithLocale: (id) locale :
"Returns a string representation of the receiver using this language."
So change your code
NSLog(@"CurrentDate: %@", [NSDate date]);
To:
NSLog(@"CurrentDate: %@", [[NSDate date] descriptionWithLocale:[NSLocale currentLocale]]);
Which should lead to what you are looking for. And I can also prove that [NSDate date] does give the correct date, but just displays with the wrong method:
We can use [today] (Wim Haanstra) to create two dates.
dateLastDay : 2010-11-02 23:59:00 +01
dateToday : 2010-11-03 24:01:00 +01
Then we use the following code to show two dates:
NSLog(@"CurrentDate: %@", dateLastDay); NSLog(@"CurrentDate: %@", dateToday);
Or:
NSLog(@"CurrentDate: %@", [dateLastDay description]); NSLog(@"CurrentDate: %@", [dateToday description]);
Two groups show the same results: "2010-11-02 22:59:00 +0000" and "2010-11-02 23: 01: 00 +0000". It seems that the two dates have the same "day", but really?
Now we compare the days of the dates:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger unitFlags = NSDayCalendarUnit; NSDateComponents *lastDayComponents = [gregorian components:unitFlags fromDate:dateLastDay]; NSDateComponents *todayComponents = [gregorian components:unitFlags fromDate:dateToday]; NSInteger lastDay = [lastDayComponents day]; NSInteger today = [todayComponents day]; return (lastDay == today) ? YES : NO;
We will get NO! Although the two dates seem to have the same day, month, and year, they DO NOT. This only appears because we displayed them wrongly.