user5047907
user5047907

Reputation:

Display open or close dependent on the day and the time

I am currently using some script which I found on a previous post.. It is to display open or close depending on the day and time..

$(document).ready(function() {
    "use strict";
var Now = new Date();
var CurrentDay = Now.getDay();
// opening time - 24 hours so 9:30am is 9, 30
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 8);
// closing time - 24 hours so 5:30pm is 17, 30
var ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 20);
var Open = (Now.getTime() > OpeningTime.getTime() && Now.getTime() < ClosingTime.getTime());
// days 0.sun 1.mon 2.tues 3.wed 4.thur 5.fri 6.sat 
// CurrentDay !== 0 && the # is the day to eclude, so if I want to be closed on Sat6, Sun0, Wed3
// CurrentDay !== 6 && CurrentDay !== 0 && CurrentDay !== 3 && Open
if (CurrentDay !== 1 && CurrentDay !== 5 && Open) {
    $('.openstatus').toggle();
}
});

Currently have it set to Mon - Fri 8am - 8pm .. But i would also like to show the open text on a saturday between 9am-5pm and sunday 10am - 4pm

Any suggestions would be much appreciated.

Thank you very much.

https://jsfiddle.net/xncor0b8/

Upvotes: 1

Views: 1993

Answers (3)

Didier Aupest
Didier Aupest

Reputation: 3367

I have a suggestion here: https://jsfiddle.net/xncor0b8/3/

var startingHour = {
    1: 8,
    2: 8,
    3: 8,
    4: 8,    
    5: 8,        
    6: 9,
    0: 10
};
var startingMin = {
    1: 0,
    2: 0,
    3: 0,
    4: 0,    
    5: 0,        
    6: 0,
    0: 0
}

var closingHour = {
    1: 20,
    2: 20,
    3: 20,
    4: 20,    
    5: 20,        
    6: 17,
    0: 16
};

var closingMin = {
    1: 0,
    2: 0,
    3: 0,
    4: 0,    
    5: 0,        
    6: 0,
    0: 0
 };

var CurrentDay = Now.getDay();
var startingTime = new Date(Now.getFullYear(), Now.getMonth(),  Now.getDate(), startingHour[CurrentDay], startingMin[CurrentDay]);
var closingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), closingHour[CurrentDay], closingMin[CurrentDay]);
var Open = (Now.getTime() > startingTime.getTime() && Now.getTime() < closingTime.getTime());
if (Open) {
    $('.openstatus').toggle();
}

Defining the hours and minutes where your store is open as var, and check the current time with the configuration.

Seems to be more easy to update if opening/closing time might change.

Upvotes: 1

Chitrang
Chitrang

Reputation: 2114

Just following the same logic in your code, you can add separate opening and closing time for Saturday and Sunday.

// opening time - 24 hours so 9:30am is 9, 30
// Saturday Opening Time
if (CurrentDay == 6)
    var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 9);
// Sunday Opening Time
else if (CurrentDay == 0)
    var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 10);
// Weekday Opening Time
else
    var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 8);

JSFiddle: https://jsfiddle.net/xncor0b8/5/

Upvotes: 0

WhiteHat
WhiteHat

Reputation: 61222

Would something like this work for you?

$(document).ready(function() {
  // Date time variables
  var dtNow;       // Now
  var dtOpenWD;    // Weekday - Open
  var dtCloseWD;   // Weekday - Close
  var dtOpenSat;   // Saturday - Open
  var dtCloseSat;  // Saturday - Close
  var dtOpenSun;   // Sunday - Open
  var dtCloseSun;  // Sunday - Close
  var isOpen;      // is open?

  // Initialize date values
  dtNow = new Date();
  dtOpenWD = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 8);
  dtCloseWD = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 20);
  dtOpenSat = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 9);
  dtCloseSat = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 17);
  dtOpenSun = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 10);
  dtCloseSun = new Date(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate(), 16);

  // Determine day and calculate isOpen
  switch (dtNow.getDay()) {
    case 0:
      isOpen = (dtNow.getTime() > dtOpenSun.getTime() && dtNow.getTime() < dtCloseSun.getTime());
      break;

    case 6:
      isOpen = (dtNow.getTime() > dtOpenSat.getTime() && dtNow.getTime() < dtCloseSat.getTime());
      break;

    default:
      isOpen = (dtNow.getTime() > dtOpenWD.getTime() && dtNow.getTime() < dtCloseWD.getTime());
  }

  if (isOpen) {
    $('.openstatus').toggle();
  }
});

Upvotes: 0

Related Questions