Date formatting yyyymmdd to yyyy-mm-dd

前端 未结 9 1887
长情又很酷
长情又很酷 2020-12-17 00:36

I\'m trying to convert a date in yyyymmdd format to yyyy-mm-dd with the following code:

tdrDate = DateTime.ParseExact(dateString, \"yyyymmdd\", null).ToStrin         


        
相关标签:
9条回答
  • 2020-12-17 01:23

    The format string is case-sensitive, so "mm" is different to "MM". You are parsing minutes ("mm"), which is why the value of months ("MM") is always at the default value of 1.

    0 讨论(0)
  • 2020-12-17 01:27

    It should be:

    DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
    

    Capital 'MM' in the first date format string.

    0 讨论(0)
  • 2020-12-17 01:34

    A handy reference: SteveX Compiled: String formatting in C#

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