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.
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.
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;
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.
DateTime.Hour
is an int
that represents whole hours. –
Telluric //Completely misread. Completely re-writing
I woudl just do something Like this
int minutesToNextHour = 60 - DateTime.Now.Minutes;
int secondsToNextHour = minutesToNextHour * 60;
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)
.
3600
. –
Decadent So you'd need to subtract the 'remainder' minutes, find the difference, and multiply that by 60, right?
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;
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;
TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60));
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.
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;
}
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.
© 2022 - 2024 — McMap. All rights reserved.