Timespan between Now and Next Hour?
Asked Answered
D

11

15

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.

Drudge answered 20/4, 2011 at 16:23 Comment(0)
S
24

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;
Swartz answered 20/4, 2011 at 16:27 Comment(0)
T
8

You don't have to mess around with ceilings and floors. The DateTime.Hour property represents whole hours (it is an integer beteen 0 and 23) of the time of the day represented by the DateTime. You can use this and the DateTime.Date property to strip the components of the DateTime you don't want (sub-hour data) and then just subtract as necessary to produce a TimeSpan.

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

You can of course extract the TotalSeconds component of the resulting TimeSpan if you want the result in seconds.

Telluric answered 20/4, 2011 at 16:26 Comment(2)
This always equals 1 hour. You need to remove the minutes somehow.Rai
@smoore: No, it doesn't. It's correct. DateTime.Hour is an int that represents whole hours.Telluric
E
8

//Completely misread. Completely re-writing

I woudl just do something Like this

int minutesToNextHour = 60 - DateTime.Now.Minutes;
int secondsToNextHour = minutesToNextHour * 60;
Earwitness answered 20/4, 2011 at 16:32 Comment(0)
W
7

This seems to be the most simple:

3600 - DateTime.Now.TimeOfDay.TotalSeconds % 3600

(if you want it in whole numbers - integer - then prefix DateTime.Now... with (int).

Wensleydale answered 20/4, 2011 at 16:40 Comment(6)
it's actually: 3600 - DateTime.Now.TimeOfDay.TotalSeconds % 3600. But +1 for the shortest solution.Brice
@Brice I guess you commented an earlier version of the answer. It´s corrected :)Wensleydale
:) so many different solutions so fast - you're bound to miss something. this may not be a competition (and it IS) - but i think you should win :)Brice
oh - and i think you mean most simple :)Brice
@Brice Thanks :) English is, unfortunately, not my native tongue.Wensleydale
I think this answer would be better if you explained why the number is 3600.Decadent
G
1

So you'd need to subtract the 'remainder' minutes, find the difference, and multiply that by 60, right?

Gally answered 20/4, 2011 at 16:26 Comment(0)
B
1

How about:

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

Or (maybe clearer):

int SecondsTillNextHour = 3600 - 60*now.Minute - now.Second;
Brice answered 20/4, 2011 at 16:27 Comment(0)
L
1

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;
Legitimize answered 20/4, 2011 at 16:31 Comment(0)
H
1
TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60));
Hippogriff answered 20/4, 2011 at 16:32 Comment(0)
R
1
TimeSpan result =  (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0)).Subtract(DateTime.Now);

Basically here you are building a new DateTime that is one hour on from Now, with no minutes or seconds, then you subtract Now from this and have your result.

Redintegrate answered 20/4, 2011 at 16:35 Comment(0)
W
1

A 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;
}
Warren answered 26/4, 2011 at 20:21 Comment(0)
A
0

I would Timespan.Parse 08:30, add 1 hr to the object, then retrieve the hour part and build a new string with :00 as the minutes and reparse the new string. There may be a more efficient way to do this, but I find this technique clear to read.

Amundson answered 20/4, 2011 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.