Given a DateTime object, how do I get an ISO 8601 date in string format?

后端 未结 18 2137
暖寄归人
暖寄归人 2020-11-22 02:15

Given:

DateTime.UtcNow

How do I get a string which represents the same value in an ISO 8601-compliant format?

Note that ISO 8601 de

相关标签:
18条回答
  • 2020-11-22 02:37

    Most of these answers have milliseconds / microseconds which clearly isn't supported by ISO 8601. The correct answer would be:

    System.DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK");
    // or
    System.DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssK");
    

    References:

    • ISO 8601 specification
    • "K" Specifier
    0 讨论(0)
  • 2020-11-22 02:41

    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:

    • Standard Date and Time Format Strings (MSDN)

    • Custom Date and Time Format Strings (MSDN)

    0 讨论(0)
  • 2020-11-22 02:41

    Using Newtonsoft.Json, you can do

    JsonConvert.SerializeObject(DateTime.UtcNow)
    

    Example: https://dotnetfiddle.net/O2xFSl

    0 讨论(0)
  • 2020-11-22 02:43

    I would just use XmlConvert:

    XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.RoundtripKind);
    

    It will automatically preserve the time zone.

    0 讨论(0)
  • 2020-11-22 02:43
    DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss zzz");
    
    DateTime.Now.ToString("O");
    

    NOTE: Depending on the conversion you are doing on your end, you will be using the first line (most like it) or the second one.

    Make sure to applied format only at local time, since "zzz" is the time zone information for UTC conversion.

    image

    0 讨论(0)
  • 2020-11-22 02:44

    To format like 2018-06-22T13:04:16 which can be passed in the URI of an API use:

    public static string FormatDateTime(DateTime dateTime)
    {
        return dateTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
    }
    
    0 讨论(0)
提交回复
热议问题