How to create a DateTime object?

I have three integers: hours, minutes and seconds.

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

+6
source share
5 answers

Take a look at MSDN and look at the constructors that exist for DateTime , you will find out that this is possible:

 var theDate = new DateTime (DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, hours, minute, second); 
+14
source

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

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

See also:

+7
source

See DateTime.Today and this DateTime constructor

  DateTime today = DateTime.Today; new DateTime(today.Year, today.Month, today.Day, 10, 39, 30); 
+4
source

you have a constructor that accepts:

 DateTime(Int32, Int32, Int32, Int32, Int32, Int32) 

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

+1
source

or you can just analyze the hours / minutes / seconds using DateTime.Parse() , which will automatically generate the current date (this is also written in the documentation)

0
source

Source: https://habr.com/ru/post/885397/


All Articles