I have a bar timetable with dynamic open and closing times and I have to calculate if currentTime falls within today's opening hours.
The problem is the open and closing times aren't in the same day. How does one calculate if currentTime falls within a range of specific times across multiple days?
I'm using jquery on this project.
First, identify the starting and an ending time. The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.
To find the total hours, subtract the time the employee clocked in from when they clocked out.
To convert time to a number of hours, multiply the time by 24, which is the number of hours in a day. To convert time to minutes, multiply the time by 1440, which is the number of minutes in a day (24*60). To convert time to seconds, multiply the time time by 86400, which is the number of seconds in a day (24*60*60 ).
if you use new Date().getTime(); this will return the number of miliseconds since a particulular time (this happens to be 1st January 1970).
If you do this for both your start and end time as well as your current time, if your current time lies between start and end then it will be greater than the start miliseconds and less than the end miliseconds.
Just a note, instead of Date().getTime();
you can actually do it as +new Date;
something I learned from looking around the web. Google Wins again :). This is because the + essentially casts the date to an int.
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