Jquery/js specific time with minutes

171
March 20, 2018, at 4:16 PM

Hello i'm making a simple jquery function that keep track of what time it is and either displays if the shop is open or closed by changing the display property of my div.

But i need to incorporate minutes as well, because the shop open for example at 7:30. How do I use minutes with hours to make it work?

I thought it would like something like: && hour + minutes <= 11.30 && hour <= 18.00) ? But this doesn't work. Any help is appreciated

 var now = new Date();
    var weekday = new Array(7);
    weekday[0] = "Monday";
    weekday[1] = "Tuesday";
    weekday[2] = "Wednesday";
    weekday[3] = "Thursday";
    weekday[4] = "Friday";
    weekday[5] = "Saturday";
    weekday[6] = "Sunday";
    var checkTime = function() {
      var dayOfWeek = now.getDay();
      var hour = now.getHours();
      var minutes = now.getMinutes();

      if (( dayOfWeek == 6)) {
        $('.opening-js').css('display', 'none');
        $('.availability-status-closed').css('display', 'block');
      } 
    else if ((dayOfWeek == 0 || dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3) && hour <= 11 && hour <= 18.00) {
         $('.opening-js').css('display', 'none');
         $('.availability-status-closed').css('display', 'block');
      } 
     else if ((dayOfWeek == 0 || dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3) && hour >= 11 && hour <= 18.00) {
         $('.opening-js').css('display', 'block');
         $('.availability-status-closed').css('display', 'none');
      } 
      else {
        if ((dayOfWeek == 5) && hour >= 10.00 && hour <= 16.00) {
          $('.availability-status-open').css("display", "block");
          $('.availability-status-closed').css("display", "none");
        }
      }
    };
    setInterval(checkTime, 1000);
    checkTime();
Answer 1

Both hour and minutes in your code are integers

var hour = now.getHours();
var minutes = now.getMinutes();

So you need to use both variables

if((hour == 7 && minutes>=30) || hour>7){
   // it is 7:30 or later.
}

or, you can reduce it to the lowest common denominator... totalMinutes

var totalMinutes = (now.getHours()*60) + now.getMinutes();
var sevenThirty = (7*60)+30;
if(totalMinutes >= sevenThirty){
    // it is 7:30 or later
}
Answer 2

If you want to compare the time as a string, try to convert your hours and minutes in a single string at first.

For example

var now = new Date();
var hours = now.getHours().toString();
var minutes = now.getMinutes().length > 1 ? now.getMinutes() : "0" + now.getMinutes();
var time = hours + "." + minutes;
console.log(time); 

The other option it to compare just Date objects.

var time1130 = new Date();
time1130.setHours(11);
time1130.setMinutes(30);
console.log(now > time1130);

Or compare integers (that probably the easiest way), as it mentioned in Jamiec answer

Rent Charter Buses Company
READ ALSO
ul&gt;li&gt;ul&gt;li&gt;ul&gt;li so i want to add style only on first li inside of that li&#39;s i don&#39;t want to add those style, how? via js [on hold]

ul>li>ul>li>ul>li so i want to add style only on first li inside of that li's i don't want to add those style, how? via js [on hold]

ul>li>ul>li>ul>li so i want to add style only on first li inside of that li's i don't want to add those style, how?

130
Jquery-Table column filter dropdown list values in lowercase

Jquery-Table column filter dropdown list values in lowercase

One of the column called status in the table has dynamically created values like Good,Bad,Average in each rowsBut Column filter dropdown lists values like good,bad,average

201
Storing Objects in HTML5 localStorage

Storing Objects in HTML5 localStorage

I'd like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string

183
JqueryFileTree - Infinite loading and no response

JqueryFileTree - Infinite loading and no response

I am using the jQueryFileTree plugin, but it is not working, it's only showing the loading messageI am willing to browse the log file on the website root, by the way

265