Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate amount of time until the next hour

I'm working on a busing website project and the buses run every hour. I'm having trouble creating a widget that finds the time between now and the next hour, so that it is clear when the next bus will run. My client requires that it is in javascript. Any suggestions? Thanks in advance.

like image 397
Mike Gajda Avatar asked Feb 18 '12 01:02

Mike Gajda


People also ask

How do you calculate duration of time?

We can calculate the duration of an activity if we know the starting and finishing time. For example, if the morning assembly in a school begins at 8:00 a.m. and finishes at 8:25 a.m. the duration of assembly is the difference of finishing time and starting time. 0825 - 800 = 25 minutes.

How do you calculate minutes from start time and end time?

To calculate the total minutes between two times: Make sure to convert both times to 24-hour time format. Take starting time and subtract it from ending time. Multiply the hours by 60 to convert it into minutes and add it to any amount of minutes you have left after the subtraction.


2 Answers

To know exactly the miliseconds from now to the next hour:

function msToNextHour() {
    return 3600000 - new Date().getTime() % 3600000;
}

Please note that this will strictly tell you how many milliseconds until the NEXT hour (if you run this at 4:00:00.000 it will give you exactly one hour).

like image 101
h4lc0n Avatar answered Sep 23 '22 18:09

h4lc0n


function getMinutesUntilNextHour() { return 60 - new Date().getMinutes(); }

Note that people who's system clocks are off will miss their bus. It might be better to use the server time instead of the time on the client's computer (AKA at least partly a non-client-side-javascript solution).

like image 45
Dagg Nabbit Avatar answered Sep 23 '22 18:09

Dagg Nabbit