XML 文件中日期/时间的正确格式是什么 [英] What is the correct format to use for Date/Time in an XML file

查看:85
本文介绍了XML 文件中日期/时间的正确格式是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .NET 写入 XML 文件时,日期/时间使用什么格式?我是简单地使用 DateTime.ToString(),还是必须使用特定的格式?

What format do I use for Date/Time when writing to an XML file using .NET? Do I simply use DateTime.ToString(), or do I have to use a specific format?

推荐答案

我总是使用 ISO 8601 格式(例如 2008-10-31T15:07:38.6875000-05:00)——date.ToString("o").它也是 XSD 日期格式.这是首选格式和 标准日期和时间格式字符串,尽管如果您不想在日期和时间之间使用 'T',则可以在必要时使用手动格式字符串:date.ToString("yyyy-MM-dd HH:mm:ss");

I always use the ISO 8601 format (e.g. 2008-10-31T15:07:38.6875000-05:00) -- date.ToString("o"). It is the XSD date format as well. That is the preferred format and a Standard Date and Time Format string, although you can use a manual format string if necessary if you don't want the 'T' between the date and time: date.ToString("yyyy-MM-dd HH:mm:ss");

如果您使用从 XSD 或 Web 服务生成的类,您可以直接将 DateTime 实例分配给类属性.如果您正在编写 XML 文本,请使用上述内容.

If you are using a generated class from an XSD or Web Service, you can just assign the DateTime instance directly to the class property. If you are writing XML text, then use the above.

这篇关于XML 文件中日期/时间的正确格式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆