开发者

NSDateFormatter encoding vs decoding is not consistent

Whe开发者_C百科n 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜