admin管理员组

文章数量:1279241

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.

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.

Share Improve this question asked Jul 20, 2012 at 11:19 mynameisnotallowedmynameisnotallowed 4831 gold badge11 silver badges36 bronze badges 2
  • What format are your times? Strings? – Sjoerd Commented Jul 20, 2012 at 11:26
  • 4 +1 for actually getting away with a username like that lolol – Jon Taylor Commented Jul 20, 2012 at 11:44
Add a ment  | 

5 Answers 5

Reset to default 7

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.

You can calculate this using javascripts Date Object like :

var start = new Date(2012,6,20,13).getTime();
var now = new Date().getTime();
var end = new Date(2012,6,21,2).getTime();

if( (start < now ) && (now < end )) {
  console.log("opened");
}
else {
 console.log("closed");
}

would output opened until tomorrow 2am then it will output closed.

you can look at this JSBin Example and change the start time to see how it changes

I shall be perfectly honest, I don't know a thing about jquery, and thereby can only offer general advice.

Is the order 'fixed'? As in: In field one is always the open time, in field two is always the closing time?

If so, the answer is fairly simple...when Field two is smaller than field one, add a 'special case'. Instead of checking 'timestamp > openTime && timestamp < closeTime' check for:

if(timestamp > openTime && timestamp <= Midnight || timestamp >= 0 && timestamp < closing time) {
     //Open/Close time
}

May this help?

A more plete code sample may be:

if((closeTime > openTime && timestamp > openTime && timestamp < closeTime) || //Normal time
(timestamp > openTime && timestamp < 86399) || //Before Midnight
(timestamp > 0 && timestamp < closeTime)) {    //After Midnight
    //Do Stuff
}

'closeTime' you'd have to make sure that you're always checking the correct weekday, like Jon Taylor already stated...but to be perfectly honest, you're probably best off with his solution.

Alternatively, you'd have to check, assuming your day time is after midnight...and before open time of THIS weekday, if this rule applies to the previous day's closing time.

Just to clarify this in code:

if(timestamp < openTime && openTimePrevDay > closeTimePrevDay && timestamp > closeTimePrevDay) {
    //Bar is open
}

Get what I mean?

If the closing time is before the opening time, it crosses over the day and you have to determine it differently:

if (closingTime >= openingTime) {
    // Open during the day
    return currentTime >= openingTime && currentTime < closingTime;
} else {
    // Open across midnight
    return currentTime >= openingTime || currentTime < closingTime;
}

You may have a look at the MomentJS library. It offers a nice API for date and time handling.

本文标签: javascriptDoes currentTime fall between 1800 and 200Stack Overflow