为 XML 生成日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6314154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate datetime format for XML
提问by Nil Pun
I'm trying to generate timestamp for cXML as shown below. Is there any function in C# which I can use to format date time to: 2011-06-09T16:37:17+16:37
我正在尝试为 cXML 生成时间戳,如下所示。C#中是否有任何函数可以用来将日期时间格式化为:2011-06-09T16:37:17+16:37
e.g.
例如
<cXML payloadID="[email protected]"
timestamp="2011-06-09T16:37:17+16:37">
回答by Oded
Use the "o" format specifier - read about this one in the standard Date and Time format stringsdocumentation on MSDN.
使用“o”格式说明符 -在 MSDN 上的标准日期和时间格式字符串文档中阅读有关此说明的信息。
The pattern for this specifier reflects a defined standard (ISO 8601).
此说明符的模式反映了定义的标准 (ISO 8601)。
And:
和:
6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30.0900000
2009 年 6 月 15 日下午 1:45:30 -> 2009-06-15T13:45:30.0900000
string formatted = DateTime.Now.ToString("o");
If this is not what you want, you will need to use a custom format string- I believe you will need to do this, as the offset is not standard.
如果这不是您想要的,您将需要使用自定义格式字符串- 我相信您需要这样做,因为偏移量不是标准的。
string formatted = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK");
回答by Haukman
Yes, using DateTime.ToString("s"), see this link: Standard Date and Time Format Strings. Be aware that "s"does not include the timezone information, whereas "o"does include both fractional seconds and timezone.
是的,使用DateTime.ToString("s"),请参阅此链接:标准日期和时间格式字符串。请注意,"s"不包括时区信息,而"o"包括小数秒和时区。
You can also use the XmlConvert.ToStringmethod, where you can specify the time zone information as well.
您也可以使用该XmlConvert.ToString方法,您还可以在其中指定时区信息。
回答by Haukman
The following is an example of a date declaration in a schema:
以下是架构中日期声明的示例:
<xs:element name="start" type="xs:date"/>
An element in your document might look like this:
文档中的元素可能如下所示:
<start>2002-09-24</start>
To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date:
要指定时区,您可以通过在日期后面添加“Z”来输入 UTC 时间的日期:
<start>2002-09-24Z</start>
or you can specify an offset from the UTC time by adding a positive or negative time behind the date:
或者您可以通过在日期后面添加正时间或负时间来指定与 UTC 时间的偏移量:
<start>2002-09-24-06:00</start>
or
或者
<start>2002-09-24+06:00</start>
回答by Abdallah
You can use ToString method
您可以使用 ToString 方法
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy"; // or any format you want
Console.WriteLine(time.ToString(format));

![获取文档为空 [#document: null] 使用 DocumentBuilder 在 java 中解析 XML 后](/res/img/loading.gif)