ios - 在更改日期格式时得到错误的日期

标签 ios ios7 ios8 nsdate nsdateformatter

我在更改日期格式时收到错误的日期,我正在使用以下代码

NSDateFormatter * formatter =  [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"MMM dd, YYYY"];

NSDate * theDate = [NSDate dateWithTimeIntervalSince1970:1419929948.7490001];

NSString * dateStr = [formatter stringFromDate:theDate];

输出-

theDate is : 2014-12-30 08:59:08 +0000
dateStr is : Dec 30, 2015

谢谢

最佳答案

您想使用 yyyy 而不是 YYYY。小写的 y 是日历年,而大写的 Y 是“一年中的一周”的年份。

下面是来自the specification的解释.

F.4 Week of Year

Values calculated for the Week of Year field range from 1 to 53 for the Gregorian calendar (they may have different ranges for other calendars). Week 1 for a year is the first week that contains at least the specified minimum number of days from that year. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (if needed). For example, January 1, 1998 was a Thursday. If the first day of the week is MONDAY and the minimum days in a week is 4 (these are the values reflecting ISO 8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. However, if the first day of the week is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998. The first three days of 1998 are then part of week 53 of 1997.

关于ios - 在更改日期格式时得到错误的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28063105/

相关文章:

ios - 归档 iOS 应用程序 swift 时出错

ios - Swift:创建一个调用另一个类中的方法的按钮

swift - 如何对非矩形或圆形的 Sprite 节点进行建模

ios - 更改数字小数部分的字体

android - 是否可以通过 Intel XDK/Phonegap 在没有 Mac 的情况下开发 IOS 应用程序?

android - 创建安装应用程序并仅运行该应用程序的代码

ios - Sprite Kit 可用性 Pre iOS 7

ios - XCTestCase setUp 方法的目的是什么?

ios - 在 dismissModalViewControllerAnimated 之后调用 popToRootViewControllerAnimated

ios - "NSURLSession API is highly asynchronous"是什么意思?