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>

1

1 Answer 1

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);
1
  • 1
    shucks I was too late :D Dec 16, 2016 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.