Magic strings for converting DateTime to string Using C#

前端 未结 4 1059

I was greeted with a nasty bug today. The task is pretty trivial, all I needed to do is to convert the DateTime object to string in \"yyyymmdd\" format

4条回答
  •  无人及你
    2021-01-27 15:09

    String.Format("{0:0000}{1:00}{2:00}", dateTime.Year, dateTime.Month, dateTime.Day);
    

    You could use this instead, I prefer the terse format though. Instead of 00 you can also use MM for specific month formatting (like in DateTime.ToString()).

提交回复
热议问题