问题
As simple as
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSLog(@"Current date in event %@ %@",currentDate,[df stringFromDate:currentDate]);
But in the log
Current date in event 2013-10-01 22:00:00 +0000 02/10/2013
Why?
回答1:
When you log an NSDate
it always prints in UTC time. When you use stringFromDate:
you get a date in local time unless you change the timezone of the date formatter.
In this case you are in a timezone two hours ahead of UTC (assuming you are in Italy).
Since the NSDate
object is October 1, 2013 at 22:00 UTC, this is the same as midnight of October 2, 2013 at UTC+2. This is why you see the string showing October 2.
Here's another way to look at it. You are not seeing a 1 day difference. You are actually seeing a 2 hour difference due to your timezone. From midnight to 0200 hours your time, you will see what appears to be a 1 day difference.
来源:https://stackoverflow.com/questions/19122239/nsdateformatter-wrong-date