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.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Lots of answers, a really simple function to get the rounded minutes remaining to the next hour is:
you have the Date object in Javascript, you could do something like:
of course you will have to work more on those calculations, but that's how you work with time in javascript
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).
To know exactly the miliseconds from now to the next hour:
Please note this will strictly tell you how many second until NEXT hour (if you run this at 4:00:00.000 it will give you exactly one hour).
Either of the other two answers will work well, but are you aware of the docs available to you about all the other nice things date is JS can do for you?
Mozilla Date Docs
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date