gpt4 book ai didi

iOS 'dateFromString' 返校一年

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:28 27 4
gpt4 key购买 nike

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

我有一个使用代码获取日期设置的对象:

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

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

然后这个值稍后用于在 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];

如果能指出我犯错的地方,我们将不胜感激。

最佳答案

您应该使用 yyyy 而不是 YYYY

来自 Data Formatting Guide :

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' 返校一年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253111/

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