dateFormatFromTemplate

dateFormatFromTemplate

本文介绍了NSDateFormatter + dateFormatFromTemplate:options:locale:BUG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

请参阅下面的代码和输出。

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 New Year: %@", dateString);

您对输出的期望是什么?

What do you expect for output?

 Happy New Year: Jan 1, 2011

但结果是...

 Happy New Year: Jan 1, 2010

怎么了?请帮忙......

What's wrong? Please help...

推荐答案

模板格式字符串应为 @yyyyMMMd(年份小写y)。

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

大写Y是别的东西(参见)。

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

这篇关于NSDateFormatter + dateFormatFromTemplate:options:locale:BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 12:42