如何确定营业时间/天内显示的图像?

时间:2011-05-04 17:08:33

标签: javascript jquery html css

所以我之前已经问过这个问题,并且我收到了以下答案,这真的有帮助!但是,假设该商家在上午9:00 开始营业,并在下午5:00 PST时间(加利福尼亚州)关闭,他们将在周六关闭和星期天。

我如何调整以下内容?

另请注意,以下脚本会根据营业时间触发图像显示/显示/隐藏。因此,在太平洋标准时间上午9点时间,图像显示“我们打开”,然后在 5:00 时,图像会显示“我们已关闭”。谢谢你们,我希望我已经输入了足够的数据/信息来回答这个问题。

以下是参考Fiddle

$(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);
  }
});​

1 个答案:

答案 0 :(得分:0)

将底部的几行改为此,目前有点混乱。

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),设置已关闭状态。