0
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM YYYY"];
NSLog(@"formatted date %@",[formatter dateFromString:@"14 Oct 2013"]);

I am trying to format the date 14 Oct 2013, to get an NSDate object out of it. But for some reason, it is not formatted as expected. Here's the date object that I get after logging it on console,

formatted date 2012-12-22 18:30:00 +0000

Anything wrong I am doing here?

akshay1188
  • 1,647
  • 2
  • 17
  • 35
  • Seems like you need to set the date formatter style to "medium" before calling dateFromString, using [formatter setDateStyle:NSDateFormatterMediumStyle]; as answered in this question: http://stackoverflow.com/questions/10518659/nsdateformatter-month-in-3-letters-instead-of-full-word – Zhang Jun 26 '14 at 09:41

1 Answers1

5

You should use lowercase yyyy because uppercase one represent a week based calendar year and you should specify timezone:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter setTimeZone:gmt];
NSLog(@"formatted date %@",[formatter dateFromString:@"14 Oct 2013"]);
Greg
  • 25,317
  • 6
  • 53
  • 62