Convert dateTime to ISO format yyyy

This question already has an answer here:

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

  • There is no standard format for the readable 8601 format. You can use a custom format:

    theDate.ToString("yyyy-MM-dd HH':'mm':'ss")
    

    (The standard format "s" will give you a "T" between the date and the time, not a space.)


    To use the strict ISO8601 , you can use the s (Sortable) format string:

     myDate.ToString("s"); // example 2009-06-15T13:45:30
    

    It's a short-hand to this custom format string:

    myDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
    

    And of course, you can build your own custom format strings.

    More info:

  • Standard Date and Time format strings
  • Custom Date and Time Format Strings

  • To add a little bit more information that confused me; I had always thought the same result could be achieved like so;

    theDate.ToString("yyyy-MM-dd HH:mm:ss")
    

    However, If your Current Culture doesn't use a colon(:) as the hour separator, and instead uses a full-stop(.) it could return as follow:

    2009-06-15 13.45.30

    Just wanted to add why the answer provided needs to be as it is;

    theDate.ToString("yyyy-MM-dd HH':'mm':'ss")
    

    :-)

    链接地址: http://www.djcxy.com/p/46542.html

    上一篇: 使用C#将ISO 8601转换为日期时间对象

    下一篇: 将dateTime转换为ISO格式yyyy