Set the time tomorrow 9 a.m.

How to set a specific value DateTimefor tomorrow9:00 AM

For instance:

DateTime startTime = new DateTime.Now.AddDays(1).//setTime to 9:00 AM

Are there any SetDateTime functionality that I don't know?

+4
source share
3 answers

You can use two methods

DateTime.Today.AddDays(1).AddHours(9)
+9
source

You can use this constructorDateTime , for example:

DateTime tomorrow = new DateTime(DateTime.Now.Year,
                                 DateTime.Now.Month,
                                 DateTime.Now.Day + 1,
                                 9,
                                 0,
                                 0);
Console.WriteLine(tomorrow);

The output will be:

18.03.2014 09:00:00

As CompuChip is mentioned, this throws an exception if the current day is the last day of the month.

You can better use the DateTime.Todayproperty with AddDays(1)and AddHours(9), because it reaches midnight of the current day. How;

DateTime tomorrow = DateTime.Today.AddDays(1).AddHours(9);
+2
source
DateTime dt=DateTime.Now;
DateTime Tomorrow = new DateTime(dt.Year,dt.Month,dt.Day+1,9,0,0);
0
source

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


All Articles