iOS'dateFromString'返回一年 [英] iOS 'dateFromString' Returning One Year Off

查看:134
本文介绍了iOS'dateFromString'返回一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSDateFormatter的dateFromString,但是当它被赋予字符串'2013-03-06'时,它会吐出NSDate为'2012-03-06 00:00:00 CST'。时间不重要。我将日期格式设置为输入和输出之间完全相同。

I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.

我有一个对象,它使用代码设置日期:

I have an object which gets a date set using the code:

NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];

[dateFormat setDateFormat:@"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];

然后此值用于在UIDatePicker上设置日期:

And then this value is later used to to set the date on a UIDatePicker:

NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"YYYY-MM-dd"];

date = [dateFormat dateFromString:myFilter.myValue];

[datePicker setDate:date animated:YES];

我非常感谢任何关于我犯错的地方的指示。

Any pointers on where I am erring would be much appreciated.

推荐答案

你应该使用 yyyy 而不是 YYYY

来自数据格式指南


常见错误是使用YYYY。 yyyy指定日历年
而YYYY指定年份(年度周),在ISO
年周历中使用

A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar

这篇关于iOS'dateFromString'返回一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆