c#获取凌晨时间

一、获取今天凌晨时间:

  方法一:

DateTime today1 = DateTime.Now.Date;
Console.WriteLine(today1);
// 输出格式   2021/5/26 0:00:00

  方法二:

DateTime now = DateTime.Now;
DateTime today2 = new DateTime(now.Year, now.Month, now.Day);
Console.WriteLine(today2);
// 输出格式   2021/5/26 0:00:00

二,获取其它时间的凌晨时间:

  例如获取一个月前的凌晨时间

DateTime now = DateTime.Now.AddMonths(-1);
DateTime today2 = new DateTime(now.Year, now.Month, now.Day);
Console.WriteLine(today2);
// 输出格式   2021/4/26 0:00:00
原文地址:https://www.cnblogs.com/Iven-zhang/p/14814348.html