Sam B
Sam B

Reputation: 27608

dateFromString making wrong date

I am definitely not new to NSDateFormatter or NSDate but this has completely stumped me. How in the world am I getting fire date wrong?

NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
    [firedateFormat setDateFormat:@"hh:mm a EEE dd MMM, YYYY"];
    [firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];

    NSDate *fireDate1 = [firedateFormat dateFromString:@"5:30 AM Sat 27 Oct, 2012"];

    NSLog(@"fireDate1: %@ ...", fireDate1);

OUTPUT:
fireDate1: 2011-12-31 10:30:00 +0000 ...

Upvotes: 0

Views: 81

Answers (1)

shabbirv
shabbirv

Reputation: 9098

It was a smaller error, switch the YYYY to yyyy

NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
[firedateFormat setDateFormat:@"hh:mm a EEE dd MMM, yyyy"];
[firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];

NSDate *fireDate1 = [firedateFormat dateFromString:@"5:30 AM Sat 27 Oct, 2012"];

NSLog(@"fireDate1: %@ ...", fireDate1);

Output: fireDate1: 2012-10-27 10:30:00 +0000 ... (Date in GMT)

A great reference for all Date Formatters http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/

Upvotes: 2

Related Questions