我有一个变量,它被定义为DateTime
类型。我需要将它赋值为今天的日期,但时间需要是下午4点。应该怎么做?
您想要的是 DateTime.Today.AddHours(16)
DateTime.Today
将返回今天午夜的日期。
您也可以使用 Date
属性从任意 DateTime
值中删除时间。
我认为这应该可以满足你的需求...
DateTime today = DateTime.Today;
DateTime dt = new DateTime(today.Year, today.Month, today.Day, 16, 0, 0);
var anotherTime = DateTime.Today.AddHours(16.0);
查看所有DateTime的重载构造函数。
DateTime myDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 16, 0, 0);
编辑:更正。感谢Jon。:)
注:本句为注释,无需翻译。