NSDateFormatter dateFromString 不会解析特定的日期字符串

发布于 2024-08-07 22:07:18 字数 664 浏览 1 评论 0原文

美好的一天,

我有一个 NSDateFormatter ,它似乎不喜欢我试图转换的特定字符串。这个特定的实例是

2009-10-16T09:42:24.999605

我所有其他日期的格式都相同,并且可以解析。

这里是转换NSDate中的字符串的代码。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS"];

[order setModifiedAtDate:[dateFormatter dateFromString:[orderDataDictionary valueForKey:@"ModifiedAt"]]];

order.ModifiedAtDatenildateFromString 调用之后。

还有其他人遇到过他们的日期无法转换为 NSDates 的情况吗?

谢谢,

Good day,

i have an NSDateFormatter that doesn't seem to like a particular string that i am trying to convert. this particular instance is

2009-10-16T09:42:24.999605

All of my other dates are formatted the same, and can be parsed.

Here is the code that converts the string in the NSDate.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS"];

[order setModifiedAtDate:[dateFormatter dateFromString:[orderDataDictionary valueForKey:@"ModifiedAt"]]];

order.ModifiedAtDate is nil after the dateFromString call.

Has anyone else come across cases where their dates could not converted into NSDates?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-08-14 22:07:18

我能够通过从格式化程序中删除亚秒字段来获取要解析的日期:

NSString *test = @"2009-10-16T09:42:24.999605";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *testOut = [dateFormatter dateFromString:test];

我怀疑您可以更详细地找出使用此函数的亚秒字段失败的原因,因为它看起来像做同样的事情,但返回错误消息:

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ /instm/NSDateFormatter/getObjectValue:forString:range:error

无论如何,希望这能让你再次前进。

I was able to get the date to parse by removing the subsecond field from the formatter:

NSString *test = @"2009-10-16T09:42:24.999605";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *testOut = [dateFormatter dateFromString:test];

I suspect you can find out in greater detail why it failed with the subsecond field using this function as it looks like it does the same thing, but returns error messages:

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/getObjectValue:forString:range:error:

At any rate, hopefully this gets you going again.

奈何桥上唱咆哮 2024-08-14 22:07:18

您应该在小数位尝试小写“s”,如下所示:

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ssssss"]

它对我有用。

You should try lowercase "s" in decimals place, like this:

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ssssss"]

It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文