【问题标题】:Trying to get countdown with current time versus the days closing time javascript试图用当前时间与关闭时间javascript倒计时
【发布时间】:2013-06-25 00:39:43
【问题描述】:

http://jsbin.com/ocuceb/6/edit

上面的链接是完整代码所在的位置,我正在尝试获取一个倒计时计时器,以了解距离营业结束还有多少小时和分钟。

function countDown() {
  var d = new Date();
  var hour = d.getHours();
    if(hour<10){hour="0"+hour;}
      else if(hour>12){hour=hour - 12;}
      var minutes = d.getMinutes();
    if(minutes<10){minutes="0"+minutes;}
      var seconds = d.getSeconds();
    if(seconds<10){seconds="0"+seconds;}
  var open = weekday[day.getDay()].open.replace(/am/g,'');
  var close = weekday[day.getDay()].close.replace(/pm/g,'');
  open = parseInt(open,10);
  close = parseInt(close,10); 
 //confused here!  
   var timeClose = close;
   var timeRemaining = Math.floor(d.getHours() - timeClose);
   document.write('<br><br>Close At: '+timeClose+"pm<br>Time Remaining:"+timeRemaining);
  }

这就是我遇到麻烦的地方,我可以得到打开的时间和关闭的时间。原来我试过这个

 var timeClose = parseInt(close+':00:00',10);
 var difference = Math.floor(d.getDay() - timeClose);

当然这不起作用,它要么是未定义要么是 NaN。它说明了实际时间,它们关闭的时间,并显示图像,如果时间在打开到关闭时间(基本上是打开的霓虹灯)和过去关闭时(关闭的霓虹灯)......我想它会非常简单,虽然我有很多棘手的角落要通过。

【问题讨论】:

  • 我检查了你的 jsbin,它工作正常。打开:8,关闭:10
  • 是的,它确实有效:D 我正在尝试得到它,所以说它是晚上 5 点,它将显示 3 小时 0 分钟,直到我们关闭。
  • 他们不是晚上 10 点关门,而不是晚上 8 点吗? :)我会调查
  • 不管怎样大声笑,你知道我想说什么:D
  • 这是你想要的吗? jsbin.com/ocuceb/20/edit

标签: javascript jquery math time parseint


【解决方案1】:

JavaScript 时间不考虑 12 小时格式。它以 24 小时格式思考。更改您的对象数组以反映(22 是晚上 10 点):

hours[0]= {open:"8:00:00",close:"22:00:00"};
hours[1]={open:"8:00:00",close:"22:00:00"};
hours[2]={open:"8:00:00",close:"22:00:00"};
hours[3]={open:"8:00:00",close:"22:00:00"};
hours[4]={open:"8:00:00",close:"22:00:00"};
hours[5]={open:"8:00:00",close:"22:00:00"};
hours[6]={open:"8:00:00",close:"22:00:00"};

此外,像这样解析 int 可能会导致问题:

var timeClose = parseInt(close+':00:00',10);

您应该 substring 冒号之间的所有内容以获得所需的小时或分钟。

var timeClose = parseInt(open.substring(0,open.indexOf(":")),10);

根据您的设置方式,在营业时间(或晚上 10 点之前),您将始终得到一个负数,因为您从关闭时间中减去当前时间。如果是晚上 8 点,关门时间是晚上 10 点,我们还剩 -2 小时?将操作数切换为从 time 中减去 getHours

var timeRemaining = Math.floor(timeClose - d.getHours());

之后,您可以检查timeRemaining 是否为负值。如果为负数,则表示业务已关闭,您可以修改输出消息以反映这种情况,即

var timeRemaining = Math.floor(timeClose - d.getHours());
if (timeRemaining < 0) {
    output = "Sorry we are closed already";
} else {
    output = "You have " + timeRemaining + " to come in and shop till you drop";
}

【讨论】:

  • 好的,如果我这样做,那么以后在 div 元素中使用我说"We Close at"+ weekday(d.getDay()).close - 12 ?
  • @EasyBB,是的。这将为您提供 12 小时格式的下午时间。
  • 我收集了人们所说的一些内容并使用它,这就是我所拥有的,你觉得这很好吗?因为这是我的第一个计时码所以jsbin.com/ocuceb/21/edit
  • 不,它充满了错误。通过所有这些工作。你会发现它们中的大多数都很容易摆脱,就像右括号一样。
  • 我也不知道 :) 我认为我需要,但我只是没有正确映射它,因为我先写了当前时间然后去剩余时间。不过谢谢,我删除了工作日并更新了代码。现在需要更少的 JavaScript!
【解决方案2】:

我认为更简单的方法是这样的

var now=new Date();
var closing=new Date(now.getFullYear(),now.getMonth(),now.getDate(),21);//Set this to 10:00pm on the present day
var diff=closing-now;//Time difference in milliseconds
if(now.getHours<7){
  //It's before opening time
}
else if(diff<0){
  //It's after closing time
}
else{
  var hours=Math.floor(diff/(1000*60*60));
  diff=diff%(1000*60*60);
  var mins=Math.floor(diff/(1000*60));
  diff=diff%(1000*60);
  var secs=Math.floor(diff/(1000));
}

这里是时间对象https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date的参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多