Search code examples
c#datetimetimespan

Timespan between Now and Next Hour?


It is 8:30 and I am trying to find out how many seconds there are between now and the next whole hour (9:00). I think I just want to DateTime.Now.AddHours(1) but after I do that I think I need the "floor". How to get that value?

Thanks.


Solution

  • Just round the time of day in hours up to the next integral value:

    var timeOfDay = DateTime.Now.TimeOfDay;
    var nextFullHour = TimeSpan.FromHours(Math.Ceiling(timeOfDay.TotalHours));
    var delta = (nextFullHour - timeOfDay).TotalSeconds;