How to get the current date without the time?

后端 未结 12 1580
青春惊慌失措
青春惊慌失措 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:25

    Current Time :

    DateTime.Now.ToString("HH:mm:ss");
    

    Current Date :

    DateTime.Today.ToString("dd-MM-yyyy");
    
    0 讨论(0)
  • 2020-11-29 01:32
    DateTime.Now.ToString("dd/MM/yyyy");
    
    0 讨论(0)
  • 2020-11-29 01:33
    string now = Convert.ToString(DateTime.Now.ToShortDateString());
    Console.WriteLine(now);
    Console.ReadLine();
    
    0 讨论(0)
  • 2020-11-29 01:35

    There is no built-in date-only type in .NET.

    The convention is to use a DateTime with the time portion set to midnight.

    The static DateTime.Today property will give you today's date.

    0 讨论(0)
  • 2020-11-29 01:37

    Use DateTime.Today property. It will return date component of DateTime.Now. It is equivalent of DateTime.Now.Date.

    0 讨论(0)
  • 2020-11-29 01:39

    You can use following code to get the date and time separately.

    DateTime now = DateTime.Now;
    string date = now.GetDateTimeFormats('d')[0];
    string time = now.GetDateTimeFormats('t')[0];
    

    You can also, check the MSDN for more information.

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