check if "current time" is between 2 times. But also check for nights as the day before

11,085

Solution 1

You could use a simple function like this to convert your time to a number of minutes since 0:00:

function getMinutes(str) {
    var time = str.split(':');
    return time[0]*60+time[1]*1;
}

And a similar function to get the current time into the same form in order to compare:

function getMinutesNow() {
    var timeNow = new Date();
    return timeNow.getHours()*60+timeNow.getMinutes();
}

Then convert both opening and closing time and, if it happens that closing time is before opening time, add 24 hours to it.

var now = getMinutesNow();
var start = getMinutes('10:00');
var end = getMinutes('2:00');
if (start > end) end += getMinutes('24:00');

if ((now > start) && (now < end)) { // your code here

Solution 2

This is the solution I've gotten to after a bit of fiddling. At the current time of 3:24 am, it outputs the correct information. changing the now array to be [13,00] also gave the correct result of 'closed' Give it a test run through to make sure it works correctly.

Edit

jQuery included solely because I am brain dead.

Edit#2

I noticed now (9pm my time) that my conversion wasn't working, it was saying 'closed', when it shouldn't have. So far, this works for any and all numbers I've put in it to test.

var start_time 	= [20,00]
var end_time 	= [12,00]
//We've got the two start times as an array of hours/minutes values.
var dateObj 	= new Date(); //I just feel dirty making multiple calls to new Date().etc
var now 		= [dateObj.getHours(),dateObj.getMinutes()]; //Gets the current Hours/Minutes 

if(end_time[0] < start_time[0] && now[0] < start_time[0]){
    start_time[0] -= 24; //This is something I came up with because I do a lot of math.
}else if(start_time[0] > end_time[0]){
    end_time[0]+=24;
}
var el=$('#result');

var start_string = to_hms_string(start_time); //the start string converted to a string format. Made comparisons easier.
var end_string = to_hms_string(end_time); //See Above
var now_string = to_hms_string(now); //Above
console.log(start_string, now_string, end_string);
var status = (start_string < now_string && now_string < end_string) ? "Open" : "Closed";
el.html(status);


//Function to_hms_string stands for "hour-minute-second" string. First name that came up.
function to_hms_string(timearr){
  var minutes = 60+timearr[1];
  var hours = "";
  if(Math.abs(timearr[0]) < 10){
    hours = "0";
  }
  hours = (timearr[0]<0) ? "-"+hours+Math.abs(timearr[0]) : hours+timearr[0];
  return hours+":"+minutes;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result">
  PlaceHolder
</div>

Solution 3

You can do this, get current time. Then define you start time and end time based on the current time getting the year, month, date for tomorrow's date add 1 to the start's date see code below. Then you can compare the time the same fi condition you have. Good luck

var now = new Date();
var start = new Date(now.getFullYear(),now.getMonth(),now.getDate(),7).getTime();
var end = new Date(now.getFullYear(),now.getMonth(),now.getDate() + 1,2).getTime();
now = now.getTime();

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

***EDIT** You can convert the current time to millis after you get the year, month and date. Then use your current if condition.

Share:
11,085
Ali Murtaza
Author by

Ali Murtaza

Updated on June 23, 2022

Comments

  • Ali Murtaza
    Ali Murtaza almost 2 years

    I have 2 times for example: 10:00 and 1:00 now i want to check if current time... is between these 2 times in javascript.

    The problem is that the closing time in this case is a next day so its before the openingstime. How can i do this the proper way for some reason i can not get around this.

    i hav efound that this could solve it:

        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");
    }
    

    but how can i do it with 2 string formats like 10:00 and 2:00 because i do not see a option to put a time alone

    var d = new Date();
    var d = new Date(milliseconds);
    var d = new Date(dateString);
    var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);