5

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.

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

5 Answers 5

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.

10
  • Nice, simple and dynamic. :-)
    – Stian
    Commented Jul 20, 2012 at 11:26
  • You are missing the point. In some cases the closing datetime should be tomorrow, so you can not simply convert the time (2:00) to a datetime and check whether the current time is before that.
    – Sjoerd
    Commented Jul 20, 2012 at 11:34
  • @Sjoerd Uh yes you can. Timestamps take into account the date so 2am tomorrow will be greater as a timestamp than 2am today, or for that matter, any time today.
    – Jon Taylor
    Commented Jul 20, 2012 at 11:36
  • @user1487756 its quirky and looks cool, however it is apparently slower. I'm no expert on that and just thought it was cool so included it.
    – Jon Taylor
    Commented Jul 20, 2012 at 11:37
  • 1
    @Sjoerd Oh, yes.. looking at this Perf its 34% slower for me, but nevertheless i learned something new =) Commented Jul 20, 2012 at 11:45
3

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

1
  • 1
    interesting how the answers that give a full solution (meaning the OP has nothing left to do other than copy and paste) are the ones that get accepted, while other answers which get many more votes, outlining the exact same principle, posted before these spelled out answers never get accepted.
    – Jon Taylor
    Commented Jul 30, 2012 at 15:14
1

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 complete 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?

5
  • why use the or case with timestamps? Do you actually mean timestamps? If so then a more appropriate way would just be to check if the tiestamp falls between the openTimeTimestamp and closeTimeTimestamp.
    – Jon Taylor
    Commented Jul 20, 2012 at 11:31
  • @JonTaylor Yes, I am aware that actual unix timestamps are continuous and you are of course correct. But the way I understood it, he doesn't use Unix-Timestamps to determine whether it's open of closed, but rather relative timestamps reaching from 0 to 86400.
    – ATaylor
    Commented Jul 20, 2012 at 11:33
  • Well I would presume he is using some kind of date/time combination otherwise he would be unaware as to whether the closing time of 2am is indeed tomorrow or the day after or today etc. Unfortuately this means that your code would close him at anything before 2am today when in fact he may have been open till 3am this morning and only 2am tomorrow. Hope that makes sense? Im tired lol.
    – Jon Taylor
    Commented Jul 20, 2012 at 11:41
  • @JonTaylor Yeah, I know what you mean, though I'm guessing he could also...instead of using a real date/time combination, use a 'week calendar'. In that case, he would know the weekday, but not the actual unix timestamp. And of course, I missed making a critical point. Hang on, let me edit.
    – ATaylor
    Commented Jul 20, 2012 at 11:52
  • @JonTaylor Thank you :) Thanks to that upvote, I finally can vote your answer up myself. Because, as a matter of fact, it is the most applicable code of conduct :)
    – ATaylor
    Commented Jul 20, 2012 at 12:19
0

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;
}
0

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

1
  • 1
    no need for any external libraries, javascript handles this very easily using just tiemstamps.
    – Jon Taylor
    Commented Jul 20, 2012 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.