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

 

posted @ 2021-05-26 17:25  Iven_雨之恋  阅读(1657)  评论(0编辑  收藏  举报