When you NSLog
a NSDate
, it will show you the date in GMT/UTC time (hence the +0000
). This is because NSDate
objects don't, themselves, capture the time zone, only a string representation of a NSDate
object does. So assuming that this input string was in your local timezone, your formatter is correct.
If, though, this input string was really in GMT/UTC, but this string doesn't have anything in it to tell you that (e.g. a +0000
or a Z
), then you have to explicitly tell the the NSDateFormatter
that you know that it wasn't necessarily a local timezone, but rather that you know that the original string was in a particular timezone, e.g.:
NSString *string = @"Tuesday, February 3, 2015 10:54:39 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"EEEE, MMMM d, y hh:mm:ss a";
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *myDate = [formatter dateFromString:string];
Just remember that whenever you NSLog
a NSDate
, it will show it to you in GMT/UTC. If you want to show a NSDate
in your local time zone, then use NSDateFormatter
method stringFromDate:
(without specifying a timeZone
property of the formatter, thereby defaulting to you local timezone).
By the way, for additional considerations when parsing time zone from a web service, see Apple Technical Q&A 1480. Notably, when parsing a date that you know is in Gregorian calendar, regardless of what calendar format the user is using, then you may also want to explicitly specify the locale
of the formatter:
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];