【问题标题】:Issues with Time math in calculating dates计算日期时的时间数学问题
【发布时间】:2012-12-04 03:50:45
【问题描述】:

我有一个倒计时here,它倒计时到新年的罚款。但是,由于某种原因,当我输入不同的日期时,完全相同的代码无法工作here;正好是 1 个月的假期。

为什么会这样?

具体来说,这是我用来获取日期、时间等的代码:

christmas = new Date(new Date().getFullYear(), 12, 25);

seconds = Math.floor((christmas - (new Date()))/1000);
minutes = Math.floor(seconds/60);
hours = Math.floor(minutes/60);
days = Math.floor(hours/24);

hours = hours-(days*24);
minutes = minutes-(days*24*60)-(hours*60);
seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);

【问题讨论】:

标签: javascript jquery html date time


【解决方案1】:

您的问题在这里:

> christmas = new Date(new Date().getFullYear(), 12, 25);

Javascript 月份索引为零:

var christmas = new Date(new Date().getFullYear(), 11, 25);

【讨论】:

  • 噢噢噢!这解释了很多!谢谢:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 2021-07-18
  • 2011-09-24
  • 2014-03-04
  • 2018-02-27
  • 1970-01-01
相关资源
最近更新 更多