NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[_dateFormatter setDateFormat:@"dd-MM-yy HH:mm"];
NSString *dateString = [_dateFormatter stringFromDate:date];
NSLog(@"Date: %@ formatted: %@", date, dateString);
[_dateFormatter release];
Gives me wrong date formatted:
Date: 2013-01-31 18:00:00 +0000 formatted: 31-01-13 19:00 (from timestamp: 1359655200.000000)
Online conversion tool: http://www.onlineconversion.com/unix_time.htm indicates that 1st date is correct: 2013-01-31 18:00:00 +0000.
Why NSDateFormatter produces different date that NSLog? Shouldn't [NSDate description]
use defaultTimeZone when printing to console? I get the same error with [_dateFormatter setTimeZone:[NSTimeZone localTimeZone]]
; and [_dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]
;
This is correct behavior, because:
[NSDate description]
uses UTC, not the local time zone (that's why there's a +0000
in the description).NSDateFormatter
is using the local time zone.The only way these will match up is if you tell NSDateFormatter
to use UTC. That might not be what you want, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With