NSDateFormatter返回错误的年份 [英] NSDateFormatter returning wrong year
问题描述
我正在使用一种非常基本的方法将日期转换为字符串.
I am using a pretty basic method to turn dates into strings.
+(NSString*)stringForDate:(NSDate*)date {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ddMMYYYY"];
NSLog(@"%@",date);
NSLog(@"%@",[dateFormat stringFromDate:date]);
return [dateFormat stringFromDate:date];
}
但是其中一些人来了错误的岁月.这是日志的示例.
But some of them are coming back with the wrong years. Here's a sample of the log.
2014-05-25 00:26:46.050 App Name[1868:60b] 2013-12-26 16:00:00 +0000
2014-05-25 00:26:46.051 App Name[1868:60b] 27122013
2014-05-25 00:26:46.051 App Name[1868:60b] 2013-12-27 16:00:00 +0000
2014-05-25 00:26:46.051 App Name[1868:60b] 28122013
2014-05-25 00:26:46.051 App Name[1868:60b] 2013-12-28 16:00:00 +0000
2014-05-25 00:26:46.052 App Name[1868:60b] 29122014
2014-05-25 00:26:46.052 App Name[1868:60b] 2013-12-29 16:00:00 +0000
2014-05-25 00:26:46.054 App Name[1868:60b] 30122014
忽略这一事实,即由于时区的原因,几天似乎晚了一天.但是,输入的所有日期都是2013年,但是有些字符串会在2014年返回.
Ignore the fact that the days appear to be a day later - that's expected, due to time zone. However, all the dates being fed in are 2013, but some of the strings are coming back with 2014.
任何帮助表示赞赏-我正在努力!
Any help appreciated - it's doing my head in!
推荐答案
问题是YYYY
和yyyy
是两个完全不同的事物.您正在使用YYYY
,正如 docs 告诉您的那样,不一定与日历年相同.
The thing is that YYYY
and yyyy
are two quite different things. You are using YYYY
, which, as the docs tell you, is not necessarily the same as the calendar year.
这篇关于NSDateFormatter返回错误的年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!