我有三个整数:小时、分钟和秒。
我希望使用上述三个变量提供的系统日期和时间创建一个DateTime
对象。
查看MSDN并查看DateTime
存在的构造函数,你会发现这是可能的:
var theDate = new DateTime (DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, hours, minute, second);
DateTime.Today
来获取午夜时刻的当前日期,并通过使用 TimeSpan
来添加所需的小时数,这是一种很好的表示一天中小时数的方式:TimeSpan time = new TimeSpan(12, 20, 20); // hours, minutes, seconds
DateTime todayWithTime = DateTime.Today + time;
参见:
DateTime today = DateTime.Today;
new DateTime(today.Year, today.Month, today.Day, 10, 39, 30);
today.Day
作为第三个参数。可能是打错了。 - kuskmen您有一个带参数的构造函数:
DateTime(Int32, Int32, Int32, Int32, Int32, Int32)
初始化一个新的 DateTime 结构实例,以指定的年、月、日、小时、分钟和秒。
或者你可以使用DateTime.Parse()
来解析小时/分钟/秒,它会自动生成当前日期(这也写在文档中)