Time interval between Now and Next Hour?

It's 8:30, and I'm trying to figure out how many seconds are between this and the next whole hour (9:00). I guess I just want DateTime.Now.AddHours(1 ), but after that I think I need a "gender". How to get this value?

Thanks.

+7
source share
11 answers

Just round the time of day in hours to the following integral value:

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

This seems the easiest:

 3600 - DateTime.Now.TimeOfDay.TotalSeconds % 3600 

(if you want it in integers - an integer, then the DateTime.Now... prefix with (int) .

+4
source

You do not need to bother with ceilings and floors. The DateTime.Hour property represents the whole hours (this is an integer from 0 to 23) of the time of day represented by DateTime. You can use this and the DateTime.Date property to strip the DateTime components that you don't want (sub -hour), and then simply subtract if necessary to create a TimeSpan .

 var now = DateTime.Now; var timeToNextHour = now.Date.AddHours(now.Hour + 1) - now; 

Of course, you can extract the TotalSeconds component of the resulting TimeSpan if you want to get the result in seconds.

+3
source

// Absolutely wrong. Completely rewrite

I'll just do something like this

 int minutesToNextHour = 60 - DateTime.Now.Minutes; int secondsToNextHour = minutesToNextHour * 60; 
+3
source

So, you will need to subtract the minutes of the remainder, find the difference and multiply by 60, right?

+1
source

What about:

 var now = DateTime.Now; int secondsTillNextHour = (60 - now.Minute)*60+(60-now.Second); 

Or (perhaps clearer):

 int SecondsTillNextHour = 3600 - 60*now.Minute - now.Second; 
+1
source

How about this:

  var currentTime = DateTime.Now; var hour = currentTime.AddHours(1).Hour; var newTime = Convert.ToDateTime(hour + ":00"); var timespan = newTime.Subtract(currentTime); var secondsDiff = timespan.TotalSeconds; 
+1
source
 TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60)); 
+1
source

I would Timespan.Parse 08:30, add 1 hour to the object, then get the hour part and build a new line with: 00 as minutes and repeat the new line. There may be a more efficient way to do this, but I find this technique readable.

0
source
 TimeSpan result = (new DateTime(DateTime.Now.Year, DateTime.Now.Millisecond, DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0)).Subtract(DateTime.Now); 

Basically, here you create a new DateTime, which is one hour from Now, without minutes or seconds, then you subtract from this and get the result.

0
source

More readable version:

 public double SecondsToNextHour() { return SecondsToNextHour( DateTime.Now ); } public double SecondsToNextHour( DateTime moment ) { DateTime currentHour = new DateTime( moment.Year, moment.Month, moment.Day, moment.Hour, 0, 0 ); DateTime nextHour = currentHour.AddHours( 1 ); TimeSpan duration = nextHour - moment; return duration.TotalSeconds; } 
0
source

All Articles