-1

I'm trying to make a countdown until a specified time on a specified date and I am having trouble converting the difference between the current time and the time specified. I am quite inexperienced in Javascript and am still learning the basics.

My HTML:

 <h1> Input the date and time you want to countdown to!</h1>
    <form>
        Second:<input id="seconds" type="number"><br>
        Minute:<input id="minutes" type="number"><br>
        Hour:<input id="hours" type="number"><br>
        Day:<input id="days" type="number"><br>
        Month:<input id="months" type="text"><br>
        Year:<input id="years" type="number"><br>
    </form>
    <button onclick="calculate()">Calculate!</button>
    <h1 id="yearsres"></h1>Years<br>
    <h1 id="monthsres"></h1>Months<br>
    <h1 id="daysres"></h1>Days<br>
    <h1 id="hoursres"></h1>Hours<br>
    <h1 id="minutesres"></h1>Minutes<br>
    <h1 id="secondsres"></h1>Seconds<br>

My Script:

function calculate() {
    var year = document.getElementById("years").value;
    var month = document.getElementById("months").value;
    var day = document.getElementById("days").value;
    var hour = document.getElementById("hours").value;
    var minute = document.getElementById("minutes").value;
    var second = document.getElementById("seconds").value;
    var countdownto = new Date(month + " " + day + "," + " " + year + " " + hour + ":" + minute + ":" + second);
    var epochto = countdownto.getTime()/1000.0;
    var current = new Date();
    var epochcurrent = current.getTime()/1000.0;
    var epochcountdown = epochto - epochcurrent;
    var hh = epochcountdown/3600
    console.log(epochto);
    console.log(epochcurrent);
    console.log(hh);
}

1 Answers1

0

To create a date from the variables, you should use below syntax

var countdownto = new Date(year,month-1,day,hour,minute,second);

Cross check the time by adding a temporary msgbox line into your code

window.alert("countdownto  : " + countdownto);
Raju
  • 2,902
  • 8
  • 34
  • 57