Search code examples
javascriptcountdown

Countdown.js adding one extra month out of nowhere?


Why does the following not work?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
    <h2 class="counter">
        Time to start: <span id="counter-text"></span>
    </h2>

    <script src="../js/countdown.js"></script>
    <script src="../js/main.js"></script>
</body>

Main.js:

function setTime() {
    var timeUntil = countdown(Date.now(), new Date(2016, 12, 3, 18, 0, 0, 0));

    document.getElementById('counter-text').textContent = timeUntil;
}

setInterval(setTime, 200);

It represents it as if it was until 3rd of January, not December. Same happens if I try November, October... always one month ahead.

As of today, it outputs:

Time to start: 85 days, 5 hours, 46 minutes and 47 seconds

instead of expected

Time to start: 54 days, 5 hours, 46 minutes and 47 seconds


Solution

  • Months are zero-based in Javascript

    function setTime() {
        var timeUntil = countdown(Date.now(), new Date(2016, 11, 3, 18, 0, 0, 0));
    
        document.getElementById('counter-text').textContent = timeUntil;
    }
    
    setInterval(setTime, 200);