Home > database >  Text displayed before the end of opening hours
Text displayed before the end of opening hours

Time:10-27

This is a well working open-close script. What I would like to do is to have another text appear 15 minutes before closing time, saying that we are closing soon. Could you help me to complete the code?

var a = document.getElementById("telefon");
var d = new Date();
var n = d.getDay();
var now = d.getHours()   "."   d.getMinutes();
var telefonweekdays = {
    0: null, //Sunday
    1: [8.30, 16.30],
    2: [8.30, 16.30],
    3: [8.30, 18.30],
    4: [8.30, 16.30],
    5: [8.30, 16.30],
    6: null //Saturday
};
var telefondayWorkingHours = telefonweekdays[n]; //Today working hours. if null we are close


if (telefondayWorkingHours && (now > telefondayWorkingHours[0] && now < telefondayWorkingHours[1])) {
    a.innerHTML = "Most nyitva."; 
       a.style.backgroundColor = '#e2efd9'; 
       a.style.color = '#538135';
       a.style.fontWeight = 'bold';
       a.style.padding = '0 4px';
       a.style.borderRadius = '3px';
       a.style.marginLeft ='5px';
  } 
  else {
     a.innerHTML = "Most zárva.";
       a.style.backgroundColor = '#efd9df'; 
       a.style.color = '#c91f41';
       a.style.fontWeight = 'bold';
       a.style.padding = '0 4px';
       a.style.borderRadius = '3px';
       a.style.marginLeft ='5px';
}

CodePudding user response:

As your values are hard coded you can check with 16.15 which is 15 minutes less than your closing time.

    var a = document.getElementById("telefon");
   
    var d = new Date();
    var n = d.getDay();
    var now = d.getHours()   "."   d.getMinutes();
    console.log(now)
    var telefonweekdays = {
        0: null, //Sunday
        1: [8.30, 16.30],
        2: [8.30, 16.30],
        3: [8.30, 18.30],
        4: [8.30, 16.30],
        5: [8.30, 16.30],
        6: null //Saturday
    };
    var telefondayWorkingHours = telefonweekdays[n]; //Today working hours. if null we are close


    if (telefondayWorkingHours && (now > telefondayWorkingHours[0] && now < telefondayWorkingHours[1])) {
            if (now == "16.15"){
          a.innerHTML == "Hamarosan bezár";
            alert("hello") // your logic for 15 mins before
        }
            
        a.innerHTML = "Most nyitva."; 
           a.style.backgroundColor = '#e2efd9'; 
           a.style.color = '#538135';
           a.style.fontWeight = 'bold';
           a.style.padding = '0 4px';
           a.style.borderRadius = '3px';
           a.style.marginLeft ='5px';
      } 
      else {
         a.innerHTML = "Most zárva.";
           a.style.backgroundColor = '#efd9df'; 
           a.style.color = '#c91f41';
           a.style.fontWeight = 'bold';
           a.style.padding = '0 4px';
           a.style.borderRadius = '3px';
           a.style.marginLeft ='5px';
    }

CodePudding user response:

let's say you like hard coded dates. you can create a function to check if the current time is during or beyond 15 minutes before closing time since your time outs have different times

var d = new Date();
var n = d.getDay();
var now = d.getHours()   "."   d.getMinutes();
var telefonweekdays = {
  0: null, //Sunday
  1: [8.3, 16.3],
  2: [8.3, 16.3],
  3: [8.3, 18.3],
  4: [8.3, 16.3],
  5: [8.3, 16.3],
  6: null, //Saturday
};

var telefondayWorkingHours = telefonweekdays[n];

if (telefondayWorkingHours && now > telefondayWorkingHours[0] && now < telefondayWorkingHours[1]) {
  // check if current time is during or beyond 15 minutes before closing time
  if (isFifteenMinBeforeOut(now, telefondayWorkingHours[1])) {
    console.log("we are closing soon");
  } else {
    console.log("we are still open");
  }
} else {
  console.log("we are close");
}

function isFifteenMinBeforeOut(current, clockout) {
  return current >= clockout - 0.15;
}

but I recommend you to try moment.js. there are lots of function you can use.

  • Related