Snowy
Snowy

Reputation: 6132

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.

Upvotes: 15

Views: 16557

Answers (11)

Ani
Ani

Reputation: 113472

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.

Upvotes: 9

Stuart
Stuart

Reputation: 1151

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.

Upvotes: 1

user687474
user687474

Reputation:

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;
}

Upvotes: 1

Lasse Espeholt
Lasse Espeholt

Reputation: 17792

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).

Upvotes: 7

NightDweller
NightDweller

Reputation: 923

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;

Upvotes: 1

Priyank
Priyank

Reputation: 10623

TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60));

Upvotes: 1

Bill Martin
Bill Martin

Reputation: 4943

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;

Upvotes: 1

AllenG
AllenG

Reputation: 8190

//Completely misread. Completely re-writing

I woudl just do something Like this

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

Upvotes: 8

Pete
Pete

Reputation: 31

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.

Upvotes: 0

dtb
dtb

Reputation: 217411

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;

Upvotes: 24

John Batdorf
John Batdorf

Reputation: 2542

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

Upvotes: 1

Related Questions