Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating DateTime in C#

Tags:

c#

datetime

How would I dynamically create a DateTime object, that always has what the current month/day/year is, but always has the 16:00 time component?

like image 785
slandau Avatar asked Aug 18 '11 14:08

slandau


2 Answers

You can do this:

 var today = DateTime.Today.AddHours(16);

That should be whatever today is at 16:00 (4pm)

like image 88
Tejs Avatar answered Sep 19 '22 12:09

Tejs


How about:

DateTime today = DateTime.Today;
DateTime Today16 = new DateTime(today.Year, today.Month, today.Day,
                                16, 0, 0)

That should use the year, month, day of today, and set time to 16:00 hours.

like image 43
marc_s Avatar answered Sep 20 '22 12:09

marc_s