Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 500
Ali Murtaza Avatar asked Sep 21 '14 00:09

Ali Murtaza


2 Answers

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
like image 67
fstanis Avatar answered Oct 03 '22 10:10

fstanis


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>
like image 40
Jhecht Avatar answered Oct 03 '22 09:10

Jhecht