本文介绍了NSDateFormatter显示错误的一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即时通讯使用xcode 4.5(4G182)和iOS 6. NSDateFormatter在iOS 6中显示错误年份,如何解决?
im using xcode 4.5(4G182) with iOS 6. NSDateFormatter show wrong year in iOS 6, how to solve?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@ == %@",str,[[dateFormatter dateFromString:str] description]);
打印出来 2012 -09-14 == 2011 -09-13 16:00:00 +0000
it print out "2012-09-14 == 2011-09-13 16:00:00 +0000"
推荐答案
YYYY
与 yyyy
不同。
根据 引用;
`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.
执行句是最后一句。请改用 yyyy
。
The operative sentence being the last one. Use yyyy
instead.
有关如何以及为何使用 YYYY
时年份值可能会有所偏差:
Further details on how and why the year values may deviate when using YYYY
:
示例:
12月29日星期一 2008 写成 2009 -W01-1
Monday 29 December 2008 is written "2009-W01-1"
周日1月3日 2010 写成 2009 -W53-7
Sunday 3 January 2010 is written "2009-W53-7"
来自
(粗体样式添加)
这篇关于NSDateFormatter显示错误的一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!