Best way to create a Midnight DateTime in C#

前端 未结 7 1170
南旧
南旧 2020-12-29 01:00

I need to create a midnight DateTime

I\'ve just done this:

DateTime endTime = DateTime.Now;
endTime.Subtract(endTime.TimeOfDay);

Ha

7条回答
  •  情书的邮戳
    2020-12-29 01:14

    You can use DateTime.Today with exact seconds of the midnight.

        DateTime today = DateTime.Today;
        DateTime mid = today.AddDays(1).AddSeconds(-1);
        Console.WriteLine(string.Format("Today: {0} , Mid Night: {1}", today.ToString(), mid.ToString()));
    
        Console.ReadLine();
    

    This should print :

    Today: 11/24/2016 10:00:00 AM , Mid Night: 11/24/2016 11:59:59 PM
    

提交回复
热议问题