NSDateFormatter dateFromString 不会解析特定的日期字符串
美好的一天,
我有一个 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.ModifiedAtDate
是nildateFromString 调用之后。
还有其他人遇到过他们的日期无法转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够通过从格式化程序中删除亚秒字段来获取要解析的日期:
我怀疑您可以更详细地找出使用此函数的亚秒字段失败的原因,因为它看起来像做同样的事情,但返回错误消息:
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:
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.
您应该在小数位尝试小写“s”,如下所示:
它对我有用。
You should try lowercase "s" in decimals place, like this:
It worked for me.