0

I have used NSDateFormatter to set dateFromString as below

NSString *string = @"Tuesday, February 3, 2015 10:54:39 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"EEEE, MMMM d, y hh:mm:ss a"];
NSDate *myDate = [formatter dateFromString:string];
NSLog(@"myDate is: %@", [myDate description]);

Output is: myDate is: 2015-02-03 17:24:39 +0000

Am I missing something here.

Napier
  • 105
  • 8
  • have you update your system time? – princ___y Dec 25 '15 at 07:49
  • The 5.5 hour time difference suggests UTC (GMT) versus Indian Stretchable Time. – Paul R Dec 25 '15 at 07:50
  • @Rob, I assumed that it will log the exact time from string without considering timezone(WYSWYG). Thanks for clarification. Also by saying "in your local timZone", do you mean, whatever system default timeZone is set to. One implementation for all devices in different time zone. – Napier Dec 25 '15 at 08:04

2 Answers2

0

your output date converted in UTC formate so you have to add time different between GMT time to UTC time as follow

NSString *string = @"Tuesday, February 3, 2015 10:54:39 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"EEEE, MMMM d, y hh:mm:ss a"];
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
NSDate *myDate = [formatter dateFromString:string];
myDate = [NSDate dateWithTimeInterval:currentGMTOffset sinceDate:myDate] ;
NSLog(@"myDate is: %@", [myDate description]);
Jaydeep Patel
  • 1,699
  • 17
  • 30
0

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"];
Rob
  • 415,655
  • 72
  • 787
  • 1,044