How to get the current date without the time?

后端 未结 12 1661
青春惊慌失措
青春惊慌失措 2020-11-29 00:50

I am able to get date and time using:

DateTime now = DateTime.Now;

How can I get the current date and time separately in the DateTime forma

12条回答
  •  盖世英雄少女心
    2020-11-29 01:17

    See, here you can get only date by passing a format string. You can get a different date format as per your requirement as given below for current date:

    DateTime.Now.ToString("M/d/yyyy");
    

    Result : "9/1/2015"

    DateTime.Now.ToString("M-d-yyyy");
    

    Result : "9-1-2015"

    DateTime.Now.ToString("yyyy-MM-dd");
    

    Result : "2015-09-01"

    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
    

    Result : "2015-09-01 09:20:10"

    For more details take a look at MSDN reference for Custom Date and Time Format Strings

提交回复
热议问题