如何转换“2012-02-06T23:18:17Z”到日期时间
我正在创建一个应用程序来解析 C# 中 iTunes 提供的 xml,但我在字段“添加日期”和“修改日期”方面遇到一些问题,这些字段的语法如下:
2012-02-06T23:18:17Z
i'我尝试过这个:
DateModified = DateTime.ParseExact(s.Element("DateModified").ToString(), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
但是应用程序失败并出现 FormatException
{"String was not recognized as a valid DateTime."}
I'm creating an application to parse the xml provided by iTunes in C# and i'm having some issues with the fields 'Date Added' and 'Date Modified' which have this syntax:
2012-02-06T23:18:17Z
i've tried this:
DateModified = DateTime.ParseExact(s.Element("DateModified").ToString(), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
but the application fails with an FormatException
{"String was not recognized as a valid DateTime."}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信
s.Element("DateModified").ToString()
没有输出您认为的内容。这按预期工作:
I believe
s.Element("DateModified").ToString()
is not outputting what you think it is.This works as expected:
如果您使用 LINQ to XML,则只需将元素转换为
DateTime
即可;这会自动使用正确的格式字符串解析日期和时间:否则,您可以使用
XmlConvert.ToDateTime
:If you are using LINQ to XML, then you can just cast the element to
DateTime
; this automatically parses the date and time with the correct format string:Otherwise, you can use
XmlConvert.ToDateTime
:没什么花哨的:
Nothing fancy:
假设这是 Linq to Xml,您需要获取元素的值,因此请执行以下操作:
Assuming this is Linq to Xml you need to grab the value of the Element, so do this instead: