I have a DateTime object that is 10:00 AM
This time represents what time of day a report should be run.
I want to calculate the amount of time remaining from NOW until 10:00 AM
part of my confusion is NOW might be after 10:am or BEFORE 10am,
I keep playing around with TimeSpan, but my results are not quite right… I am sure this is simple, but it is one of those things I have been working of for a few hours and I need a push in the right direction…
I want the timespan object timeTillRun to be correct…here is what I have tried:
{
DateTime scheduledRun = DateTime.Today.AddHours(_timeToStart);//_timeToStart = 10
TimeSpan timeTillRun = DateTime.Now - scheduledRun;
}
This will work… but you need to reverse the order of subtraction:
Note that if it’s currently after 10AM,
timeTillRunwill be negative. You will presumably also need to check if the current time is on or after 10AM, then add 10 hours and one day toDateTime.Todayto obtain the next run time. Alternatively, you could test iftimeTillRunis negative; if so, just add one day to it (timeTillRun += new TimeSpan(1, 0, 0, 0)).