JavaScript将未来日期设置为今天

时间:2016-12-16 10:26:05

标签: javascript

我的网站上有以下脚本,可生成倒数计时器。 dateFuture设置为今天午夜的日期。是否可以将其设置为当前日期,以便我们不必每天都进行更改?

谢谢。



var today = new Date();
dateFuture = new Date(2016, 11, 16, 23, 59, 59);

function GetCount() {

  dateNow = new Date();
  amount = dateFuture.getTime() - dateNow.getTime();
  delete dateNow;
  if(amount < 0) {
    document.getElementById("countbox").innerHTML = "Now!";
  } else {
    days = 0;
    hours = 0;
    mins = 0;
    secs = 0;
    out = "";

    amount = Math.floor(amount / 1000);

    days=Math.floor(amount / 86400);
    amount = amount % 86400;

    hours = Math.floor(amount / 3600);
    amount = amount % 3600;

    mins = Math.floor(amount / 60);
    amount = amount % 60;

    secs = Math.floor(amount);

    if(days != 0) {
      out += days + " day" + ((days != 1) ? "s" : "") + ", ";
    }
    if(days != 0 || hours != 0) {
      out += hours + " hour" + ((hours != 1) ? "s" : "") + ", ";
    }
    if(days != 0 || hours != 0 || mins != 0) {
      out += mins + " minute" + ((mins != 1) ? "s" : "") + ", ";
    }
    out += secs + " seconds";
    document.getElementById("countbox").innerHTML = out;

    setTimeout("GetCount()", 1000);
  }
}

window.onload = function() { GetCount(); }
&#13;
<div id="countbox"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用setHourssetMinutessetSecondssetMilliseconds来操纵日期。

var dateFuture = new Date();
dateFuture.setHours(23);
dateFuture.setMinutes(59);
dateFuture.setSeconds(59);
dateFuture.setMilliseconds(999);