What's the simplest way to format a .NET DateTime according to YYYYMMDD or the like?

后端 未结 5 945
春和景丽
春和景丽 2020-12-11 09:20

I have a DateTime class and wish to display it according to some format... in this case, I specifically want to format it as YYYYMMDD format.

What\'s the best C#/.NE

相关标签:
5条回答
  • 2020-12-11 09:55

    ToString(format)? i.e.

    string s = DateTime.Today.ToString("yyyyMMdd");
    

    (note the case)

    0 讨论(0)
  • 2020-12-11 09:55
    DateTime.Now.ToString("yyyyMMdd");
    
    0 讨论(0)
  • 2020-12-11 09:55
    DateTime.ToString(<format string>)
    

    Look at the docs for the format string details.

    0 讨论(0)
  • 2020-12-11 10:07

    I always use this site to get any dates formats etc. http://blog.stevex.net/index.php/string-formatting-in-csharp/

    0 讨论(0)
  • 2020-12-11 10:12
    myDateTime.ToString("yyyyMMdd")
    

    Here you can find a comprehesive list of DateTime patterns and pattern characters: DateTime.ToString() Patterns

    0 讨论(0)
提交回复
热议问题