給定一個 DateTime 對象,如何獲取字符串格式的 ISO 8601 日期? (Given a DateTime object, how do I get an ISO 8601 date in string format?)


問題描述

給定一個 DateTime 對象,如何獲取字符串格式的 ISO 8601 日期? (Given a DateTime object, how do I get an ISO 8601 date in string format?)

給定:

DateTime.UtcNow

如何在 符合 ISO 8601 的格式?

請注意,ISO 8601 定義了許多類似的格式。我要找的具體格式是:

yyyy‑MM‑ddTHH:mm:ssZ

參考解法

方法 1:

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", CultureInfo.InvariantCulture);

Using custom date‑time formatting, this gives you a date similar to
2008‑09‑22T13:57:31.2311892‑04:00.

Another way is:

DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);

which uses the standard "round‑trip" style (ISO 8601) to give you
2008‑09‑22T14:01:54.9571247Z.

To get the specified format, you can use:

DateTime.UtcNow.ToString("yyyy‑MM‑ddTHH:mm:ssZ", CultureInfo.InvariantCulture)

方法 2:

DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture) should give you what you are looking for as the "s" format specifier is described as a sortable date/time pattern; conforms to ISO 8601.

EDIT: To get the additional Z at the end as the OP requires, use "o" instead of "s".

方法 3:

DateTime.UtcNow.ToString("s")

Returns something like 2008‑04‑10T06:30:00

UtcNow obviously returns a UTC time so there is no harm in:

string.Concat(DateTime.UtcNow.ToString("s"), "Z")

方法 4:

Use:

private void TimeFormats()
{
    DateTime localTime = DateTime.Now;
    DateTime utcTime = DateTime.UtcNow;
    DateTimeOffset localTimeAndOffset = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));

    //UTC
    string strUtcTime_o = utcTime.ToString("o");
    string strUtcTime_s = utcTime.ToString("s");
    string strUtcTime_custom = utcTime.ToString("yyyy‑MM‑ddTHH:mm:ssK");

    //Local
    string strLocalTimeAndOffset_o = localTimeAndOffset.ToString("o");
    string strLocalTimeAndOffset_s = localTimeAndOffset.ToString("s");
    string strLocalTimeAndOffset_custom = utcTime.ToString("yyyy‑MM‑ddTHH:mm:ssK");

    //Output
    Response.Write("<br/>UTC<br/>");
    Response.Write("strUtcTime_o: " + strUtcTime_o + "<br/>");
    Response.Write("strUtcTime_s: " + strUtcTime_s + "<br/>");
    Response.Write("strUtcTime_custom: " + strUtcTime_custom + "<br/>");

    Response.Write("<br/>Local Time<br/>");
    Response.Write("strLocalTimeAndOffset_o: " + strLocalTimeAndOffset_o + "<br/>");
    Response.Write("strLocalTimeAndOffset_s: " + strLocalTimeAndOffset_s + "<br/>");
    Response.Write("strLocalTimeAndOffset_custom: " + strLocalTimeAndOffset_custom + "<br/>");

}

OUTPUT

UTC
    strUtcTime_o: 2012‑09‑17T22:02:51.4021600Z
    strUtcTime_s: 2012‑09‑17T22:02:51
    strUtcTime_custom: 2012‑09‑17T22:02:51Z

Local Time
    strLocalTimeAndOffset_o: 2012‑09‑17T15:02:51.4021600‑07:00
    strLocalTimeAndOffset_s: 2012‑09‑17T15:02:51
    strLocalTimeAndOffset_custom: 2012‑09‑17T22:02:51Z

Sources:

方法 5:

System.DateTime.UtcNow.ToString("o")

=>

val it : string = "2013‑10‑13T13:03:50.2950037Z"

(by IainWayneSimon WilsonIainDonHenrik)

參考文件

  1. Given a DateTime object, how do I get an ISO 8601 date in string format? (CC BY‑SA 2.5/3.0/4.0)

#datetime #datetime-format #iso8601 #C#






相關問題

NHibernate:HQL:從日期字段中刪除時間部分 (NHibernate:HQL: Remove time part from date field)

如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)

Памылка дадання даты пры адніманні ад 0:00 (Dateadd error when subtracting from 0:00)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

CodeReview:java Dates diff(以天為單位) (CodeReview: java Dates diff (in day resolution))

顯示兩個給定時間之間的 15 分鐘步長 (display 15-minute steps between two given times)

如何在 C# 中獲取月份名稱? (How to get the month name in C#?)

fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

setTimeout 一天中的特定時間,然後停止直到下一個特定時間 (setTimeout for specific hours of day and then stop until next specific time)

將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])

Python Dataframe 在連接時防止重複 (Python Dataframe prevent duplicates while concating)







留言討論