NSDateFormatter显示错误的一年 [英] NSDateFormatter show wrong year

查看:126
本文介绍了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 不同。

根据这个页面 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.

执行句是最后一句。请改用 yyyy

The operative sentence being the last one. Use yyyyinstead.

有关如何以及为何使用 YYYY 时年份值可能会有所偏差:

Further details on how and why the year values may deviate when using YYYY:


ISO周编号年份开始在
01周的第一天(星期一),在新的ISO年份之前的星期日结束(因此没有
重叠或差距)。它包括52或53 <完整周。 ISO
周编号年份数字与星期五,星期六和星期日的传统
公历年数相差不等,或者周六和周日或周日仅为周二的
。传统的
格里高利历年的开始(在上一个ISO
周编号年结束时)和周一,周二和周三,或周一
和周二,或者只是一个星期一,在传统的格里高利
日历年结束时(在下一个ISO周的第01周 - 编号为
年)。对于星期四,ISO周编号年份数始终是
等于传统的公历年份数。

The ISO week-numbering year starts at the first day (Monday) of week 01 and ends at the Sunday before the new ISO year (hence without overlap or gap). It consists of 52 or 53 full weeks. The ISO week-numbering year number deviates from the number of the traditional Gregorian calendar year on a Friday, Saturday, and Sunday, or a Saturday and Sunday, or just a Sunday, at the start of the traditional Gregorian calendar year (which are at the end of the previous ISO week-numbering year) and a Monday, Tuesday and Wednesday, or a Monday and Tuesday, or just a Monday, at the end of the traditional Gregorian calendar year (which are in week 01 of the next ISO week-numbering year). For Thursdays, the ISO week-numbering year number is always equal to the traditional Gregorian calendar year number.

示例:

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"

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

这篇关于NSDateFormatter显示错误的一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆