How to create a DateTime object?

前端 未结 5 1523
滥情空心
滥情空心 2020-12-18 20:07

I have three integers: Hours, Minutes, and Seconds.

I want to create a DateTime object with System.Date and Time provided by the above three variables.<

5条回答
  •  心在旅途
    2020-12-18 20:26

    You can use DateTime.Today to get the current date at midnight, and add the hours you need by using a TimeSpan, which is a good way to represent hours of the day:

    TimeSpan time = new TimeSpan(12, 20, 20); // hours, minutes, seconds
    DateTime todayWithTime = DateTime.Today + time;
    

    See also:

    • TimeSpan(Int32, Int32, Int32)
    • operator +(DateTime,TimeSpan)

提交回复
热议问题