gpt4 book ai didi

NSDateFormatter 显示错误的年份

转载 作者:行者123 更新时间:2023-11-30 10:33:15 28 4
gpt4 key购买 nike

我在 iOS 6 上使用 xcode 4.5(4G182)。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]);

打印出“2012-09-14 == 2011-09-13 16:00:00 +0000”

最佳答案

YYYYyyyy 不同。

根据this page其中iOS Date format page引用文献;

`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

<小时/>

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

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

Examples:

Monday 29 December 2008 is written "2009-W01-1"

Sunday 3 January 2010 is written "2009-W53-7"

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

关于NSDateFormatter 显示错误的年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635943/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com