NSDateFormatter显示错误的一年

Sim*_*mon 37 nsdateformatter ios6

即时通讯使用xcode 4.5(4G182)与iOS 6. NSDateFormatter在iOS 6中显示错误年份,如何解决?

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@ == %@",str,[[dateFormatter dateFromString:str] description]);
Run Code Online (Sandbox Code Playgroud)

打印出来" 2012 -09-14 == 2011 -09-13 16:00:00 +0000"

Joa*_*son 116

YYYY是不一样的yyyy.

根据该页面将IOS日期格式页面引用;

`y`: Year
`Y`: Year (in "Week of Year" based calendars). This year designation is used in 
     ISO year-week calendar as defined by ISO 8601, but can be used in 
     non-Gregorian based calendar systems where week date processing is desired. 
     May not always be the same value as calendar year.
Run Code Online (Sandbox Code Playgroud)

有效的句子是最后一句.请yyyy改用.


有关使用时年份值如何以及为何可能出现偏差的详细信息YYYY:

ISO周编号年从第01周的第一天(星期一)开始,到新ISO年份之前的星期日结束(因此没有重叠或差距).它包括52或53 整周.ISO周编号年份数与周五,周六和周日,或周六和周日,或周日,传统公历年开始时的传统公历年数不同(在传统公历年末(即下一个ISO周的第01周),周一,周二和周三,或周一和周二,或仅周一,结束前一个ISO周编号年份 - 数年).对于星期四,ISO周编号年份编号始终等于传统的公历年份编号.

例子:

2008年12 月 29日星期一写成" 2009- W01-1"

2010年 1 月 3日星期日写成" 2009- W53-7"

来自https://en.wikipedia.org/wiki/ISO_8601#Week_dates (添加了粗体样式)

  • 我认为在设计格式化程序时存在一种无法思考的因素,该格式化程序使用YYYY格式化一年"可能并不总是与日历年相同的值"."QQQQ"或"XXXX"(例如)不是一个不容易出错的选择吗?"助记符值"在这里看起来并不像是优先考虑的事情. (3认同)
  • 谢谢,我已经很久没遇到这个问题..非常感谢 (2认同)
  • 我感到很尴尬,经过3年多的iOS经验,我现在才知道! (2认同)