Format DateTime.Now to yyyy-mm-dd

前端 未结 3 458
我寻月下人不归
我寻月下人不归 2021-02-06 23:55

I want to convert the DateTime.Now to the format of yyyy-mm-dd since that is the only format that i can use in my query that i want to include.
The default

相关标签:
3条回答
  • 2021-02-07 00:21

    According to msdn MM format specifier stands for month and mm - for minutes.

    "mm" | The minute, from 00 through 59.

    "MM" | The month, from 01 through 12.

    So your code should look like the following:

        var dateString1 = DateTime.Now.ToString("yyyyMMdd");
        var dateString2 = DateTime.Now.ToString("yyyy-MM-dd");
    
        Console.WriteLine("yyyyMMdd " + dateString1);
        Console.WriteLine("yyyy-MM-dd "+ dateString2);
    

    And you will get the desired result

    0 讨论(0)
  • 2021-02-07 00:24
        var dateString1 = DateTime.Now.ToString("yyyyMMdd");
        var dateString2 = DateTime.Now.ToString("yyyy-MM-dd");
    
        Console.WriteLine("yyyyMMdd " + dateString1);
        Console.WriteLine("yyyy-MM-dd "+ dateString2);
    

    You are using "mm" instead of "MM" in your second format. mm is for minutes, MM is for month.

    0 讨论(0)
  • 2021-02-07 00:26

    Your miss is lower-case "m" in second format, that determine MINUTES, but you need "M" instead of "m" for MONTHS.

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