Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

c# - 给定一个DateTime对象,如何获取字符串格式的ISO 8601日期?(Given a DateTime object, how do I get an ISO 8601 date in string format?)

Given: (鉴于:)

DateTime.UtcNow

How do I get a string which represents the same value in an ISO 8601 -compliant format? (如何获得表示符合ISO 8601格式的相同值的字符串?)

Note that ISO 8601 defines a number of similar formats. (请注意,ISO 8601定义了许多相似的格式。) The specific format I am looking for is: (我正在寻找的特定格式是:)

yyyy-MM-ddTHH:mm:ssZ
  ask by Iain translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note to readers: Several commenters have pointed out some problems in this answer (related particularly to the first suggestion). (读者注意:一些评论者指出了此答案中的一些问题(特别是与第一个建议有关)。) Refer to the comments section for more information. (有关更多信息,请参见评论部分。)

DateTime.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");

This gives you a date similar to 2008-09-22T13:57:31.2311892-04:00 . (这给您一个类似于2008-09-22T13:57:31.2311892-04:00的日期 。)

Another way is: (另一种方法是:)

DateTime.UtcNow.ToString("o");

which gives you 2008-09-22T14:01:54.9571247Z (这给你2008-09-22T14:01:54.9571247Z)

To get the specified format, you can use: (要获取指定的格式,可以使用:)

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")

DateTime Formatting Options (日期时间格式选项)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...