views:

25

answers:

1

When I use this date formatter, it's not giving me back the same date I start with if I got from date -> string -> date.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateFormat:@"MMM d, YYYY hh:mm aaa"];

NSDate *date = [NSDate date];
NSString *string = [formatter stringFromDate:date];
NSDate *resultingDate = [formatter dateFromString:string];

My test generated a date string of Aug 24, 2010 01:00 PM. When fed back into dateFromString I get 2009-12-27 13:00:00. The time is correct, but the date is all wrong.

Anyone know what's going on here?

+3  A: 

The problem is with this statement:

[formatter setDateFormat:@"MMM d, YYYY hh:mm aaa"];

Change that to this (lowercase y):

[formatter setDateFormat:@"MMM d, yyyy hh:mm aaa"];

And it works fine.

I honestly do not know what is wrong with the code behind the NSDateFormatter, but all i know is that it works.

Richard J. Ross III
You are awesome. Thank you. I still find it strange that it working fine for date->string, but not string->date.
Squeegy