开发者

NSDateFormatter +dateFormatFromTemplate:options:locale: BUG?

开发者 https://www.devze.com 2023-01-30 20:19 出处:网络
Please see below codes and output. NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];

Please see below codes and output.

 NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
 [dateComponents setYear:2011];
 [dateComponents setMonth:1];
 [dateComponents setDay:1];

 NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"YYYYMMMd" options:0 locale:[NSLocale currentLocale]]];

 NSString *dateString = [dateFormatter stringFromDate:date];

 NSLog(@"Happy Ne开发者_运维百科w Year: %@", dateString);

What do you expect for output?

 Happy New Year: Jan 1, 2011

But the result is...

 Happy New Year: Jan 1, 2010

What's wrong? Please help...


The template format string should be @"yyyyMMMd" (lowercase "y" for year).

Uppercase "Y" is something else (see Unicode Date Format Patterns).

0

精彩评论

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