NSDateFormatter显示错误的一年

编程入门 行业动态 更新时间:2024-10-23 11:20:40
本文介绍了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"

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

更多推荐

NSDateFormatter显示错误的一年

本文发布于:2023-10-28 23:29:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   NSDateFormatter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!