NSDateFormatter outputs incorrect year for format YYYY for some dates

Originator:jehiah
Number:rdar://6468204 Date Originated:28-Dec-2008 10:01 AM
Status:Closed Resolved:
Product:iPhone SDK Product Version:
Classification: Reproducible:Always
 
Summary:

when converting NSDate objects to strings with NSDateFormatter, some dates (like 2008-12-30) output an incorrect year when output with the format YYYY.

Steps to Reproduce:

The following code block reliably reproduces this problem

NSDate *dateObj =[[NSDate alloc] initWithString:@"2008-12-30 12:47:59 -0500"];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
[formatter setLocale:uslocale];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSString *dayStr = [formatter stringFromDate:dateObj];
NSLog(@"formatter says %@ but dateObj is %@", dayStr, [dateObj description]);


Expected Results:

The formatter should output "2008-12-30"

Actual Results:

The formatter outputs "2009-12-30".

Comments

oops

troy is correct yyyy is what i expected not YYYY.

This seems to be functioning correctly

Based on the description at http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html, this API is providing the right result in the case you list. The issue is that the capital "Y" is not the year you think it is. Instead, it refers to the ISO week-numbering year (see the linked http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns).

If you want what you expect, use a lowercase "y" instead: "yyyy-MM-dd".


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!