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

后端 未结 18 2435
暖寄归人
暖寄归人 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

提交回复
热议问题