如何确定在营业时间/工作日内显示的图像

How to determine the image that is displayed during business hours/days?

本文关键字:工作日 显示 图像 时间 何确定      更新时间:2023-09-26

所以我之前已经问过这个问题了,我收到了以下的答案,这真的很有帮助!然而,假设营业时间为上午9:00 ,下午5:00 关闭太平洋标准时间(加利福尼亚州),周六和周日关闭。

我如何在下面调整它?

还请记住,下面的脚本将根据营业时间触发图像显示/显示/隐藏。所以在上午9:00 PST时间图像说的是'我们是开放的',在下午5:00 图像然后去'我们是关闭的'。谢谢大家,我希望我已经输入了足够的数据/信息让你回答这个问题。

这是一个参考提琴

$(window).load(function(){
  // Translate your hours to UTC, example here is using
  // Central Standard Time (-0500 UTC)
  // Opening hour in UTC is 16, Closing hour is 0 the next day
  var d      = new Date(), 
      open   = new Date(), 
      closed = new Date();
  // Statically set UTC date for open
  open.setUTCHours(16);
  open.setUTCMinutes(0);
  open.setUTCSeconds(0);
  open.setUTCMilliseconds(0);
  // Statically Set UTC date for closing
  closed.setUTCDate(d.getUTCDate()+1); // UTC time rotates back to 0, add a day
  closed.setUTCHours(0); // UTC hours is 0
  closed.setUTCMinutes(0);
  closed.setUTCSeconds(0);
  closed.setUTCMilliseconds(0);
  // Debugging
  console.log("user's date:" + d);
  console.log("store open time in user's timezone:" + open);
  console.log("store close time in user's timezone:" + closed);
  console.log(d > open); // user's time is greater than opening time
  console.log(d < closed); // is user's time less than closing time
                           // (you don't have to go home...)
  // Test for store open?
  if (d > open && d < closed) {
    setOpenStatus(true);
  } else {
    setOpenStatus(false);
  }
  function setOpenStatus(isOpen) {
    $('#opend').toggle(isOpen);
    $('#closed').toggle(!isOpen);
  }
});​

编辑/更新脚本

$(window).load(function(){
  // Translate your hours to UTC, example here is using
  // Central Standard Time (-0500 UTC)
  // Opening hour in UTC is 16, Closing hour is 0 the next day
  var d      = new Date(), 
      open   = new Date(), 
      closed = new Date();
  // Statically set UTC date for open
  open.setUTCHours(16);
  open.setUTCMinutes(0);
  open.setUTCSeconds(0);
  open.setUTCMilliseconds(0);
  // Statically Set UTC date for closing
  closed.setUTCDate(d.getUTCDate()+1); // UTC time rotates back to 0, add a day
  closed.setUTCHours(0); // UTC hours is 0
  closed.setUTCMinutes(0);
  closed.setUTCSeconds(0);
  closed.setUTCMilliseconds(0);
  // Debugging
  console.log("user's date:" + d);
  console.log("store open time in user's timezone:" + open);
  console.log("store close time in user's timezone:" + closed);
  console.log(d > open); // user's time is greater than opening time
  console.log(d < closed); // is user's time less than closing time
                           // (you don't have to go home...)
  // Test for store open?
  if (d > open && d < closed) {
    setOpenStatus(true);
  }
  if (d.getDay() !== 0 && d.getDay() !== 6 && (d > open && d < closed))
  else {
    setOpenStatus(false);
  }
  function setOpenStatus(isOpen) {
    $('#opend').toggle(isOpen);
    $('#closed').toggle(!isOpen);
  }
});​

把下面几行改成这样,现在有点乱

if (d.getDay() !== 0 && d.getDay() !== 6 && (d >= open && d < closed)) {
    setOpenStatus(true);
} else {
    setOpenStatus(false);
}

所以你明白了条件,它说:如果不是星期日(d.getDay() !== 0)或星期六(d.getDay() !== 6),当前时间在打开时间(d >= open)之后或在关闭时间(d < closed)之前,则设置打开状态,否则(else),设置关闭状态