1

我可以在我的应用程序中看到一个非常奇怪的日期转换。这是代码:

theDateString = [theDateFormatter stringFromDate:date];

如果date2007-12-31 00:00:00 +0100 theDateString_2008

但如果日期2004-12-31 00:00:00 +0100 theDateString2004.

该方法- (NSString *)stringFromDate:(NSDate *)dateNSDateFormatter,它是一个mac应用程序。

NSDateFormatter 格式为:[theDateFormatter setDateFormat:@"Y"];

为什么 ?谢谢

4

1 回答 1

2

您应该使用@"y"而不是@"Y",因为大写字母“Y”根据“一年中的周”指定年份。2007 年的第 52 周在 2008 年结束,这就是它返回 2008 年而不是 2007 年的原因。

更新:
见:http ://waracle.net/mobile/iphone-nsdateformatter-date-formatting-table/

于 2013-01-16T13:58:26.923 回答