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.
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.
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.
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With