gpt4 book ai didi

iphone - NSDateFormatter dateFromString 给出了错误的日期

转载 作者:IT王子 更新时间:2023-10-29 08:13:41 27 4
gpt4 key购买 nike

我已经在其他类似的帖子中尝试过这些建议,但无济于事。这是情况:

我有一个像这样的 NSString:

Fri November 18, 2011

我正在尝试将其转换为这样的日期:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EE MMMM d, YYYY"];
NSDate *myDate = [df dateFromString: dateString];

但是生成的 myDate 变量包含这个:

2010-12-31 00:00:00 +0000

有谁知道为什么从 dateFromString 中选出了错误的日期?字符串是 11 月 18 日,转换后的日期是 12 月 31 日……

谢谢大家!

最佳答案

您需要使用 yyyy 作为年份,而不是 YYYY

来自Apple reference :

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. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

为了正确,您还必须将 EE 更改为 EEE。尽管它们在我的测试中产生了相同的结果。

关于iphone - NSDateFormatter dateFromString 给出了错误的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8186833/

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