Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set time value to tomorrow 9 am

Tags:

c#

datetime

How can I set certain DateTime value to tomorrow 9:00 AM

For example:

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

Is there some SetDateTime value functionality that I don't know?

like image 987
Cybercop Avatar asked Mar 17 '14 07:03

Cybercop


2 Answers

You can use two methods

DateTime.Today.AddDays(1).AddHours(9)
like image 144
user3401335 Avatar answered Nov 15 '22 10:11

user3401335


You can use this DateTime constructor like;

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

Output will be;

18.03.2014 09:00:00

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

Better you can use DateTime.Today property with AddDays(1) and AddHours(9) because it get's to midnight of the current day. Like;

DateTime tomorrow = DateTime.Today.AddDays(1).AddHours(9);
like image 34
Soner Gönül Avatar answered Nov 15 '22 08:11

Soner Gönül