Cybercop
Cybercop

Reputation: 8674

Set time value to tomorrow 9 am

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?

Upvotes: 5

Views: 3365

Answers (3)

user3401335
user3401335

Reputation: 2405

You can use two methods

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

Upvotes: 14

Soner Gönül
Soner Gönül

Reputation: 98750

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);

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

DateTime dt=DateTime.Now;
DateTime Tomorrow = new DateTime(dt.Year,dt.Month,dt.Day+1,9,0,0);

Upvotes: 0

Related Questions