Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the exact date of the next 2:00 am in .net

I need the get a date object that specifies the next 2:00am that will come.

So pretend the time is 14:00 on the 15th, I need the date object to contain 2:00 on the 16th If the time is 1:00 on the 16th, I need the date object to contain 2:00 on the 16th

How do I do this?

like image 210
dpington Avatar asked Dec 05 '22 23:12

dpington


2 Answers

In C#

DateTime dt = DateTime.Today.AddHours(2);
if (dt < DateTime.Now)
    dt = dt.AddDays(1);

I'm sure there is a neater/cleverer way, but that will get the job done.

like image 99
fearofawhackplanet Avatar answered Dec 24 '22 09:12

fearofawhackplanet


if the currenttime is before 2:00am, then the next 2:00am will be today else the next 2:00am will occur tomorrow (day + 1).

You will probably want to use a datetime object and increment the day by one, this should ensure that you are always on the correct date.

like image 41
Tony Abrams Avatar answered Dec 24 '22 09:12

Tony Abrams