John Clark
John Clark

Reputation: 1

JavaScript set future date as today

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>

Upvotes: 0

Views: 202

Answers (1)

TryingToImprove
TryingToImprove

Reputation: 7407

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);

Upvotes: 2

Related Questions