我需要创建一个午夜DateTime
我刚刚这样做了:
DateTime endTime = DateTime.Now; endTime.Subtract(endTime.TimeOfDay);
还没有测试它,我假设它有效,但是有更好/更清洁的方式吗?
只是使用foo.Date
,或DateTime.Today
今天的日期
DateTime.Today
DateTime.Now
.AddDays(1)
.Date
DateTime endTime = DateTime.Now.Date;
现在endTime.TimeOfDay.ToString()
回来了"00:00:00"
您可以使用DateTime.Today
午夜的精确秒数.
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();
这应该打印:
Today: 11/24/2016 10:00:00 AM , Mid Night: 11/24/2016 11:59:59 PM