0

I have the below script in our website which generates a countdown timer. The dateFuture is set to todays date at midnight. Is it possible to set this as the current date so that we don't have to change this every day?

Thank you.

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(); }
<div id="countbox"></div>
Nhan
  • 3,595
  • 6
  • 30
  • 38
  • Possible duplicate? http://stackoverflow.com/questions/3894048/what-is-the-best-way-to-initialize-a-javascript-date-to-midnight – v7d Dec 16 '16 at 10:32

1 Answers1

2

You can manipulate the date by using setHours, setMinutes, setSeconds and setMilliseconds.

var dateFuture = new Date();
dateFuture.setHours(23);
dateFuture.setMinutes(59);
dateFuture.setSeconds(59);
dateFuture.setMilliseconds(999);
TryingToImprove
  • 7,047
  • 4
  • 30
  • 39