MariaL MariaL - 1 year ago 75
HTML Question

javascript countdown clock date issue

So I've written some code to display a countdown clock on my website.
The html looks like this:

<div id="deadline_Container">
<div id="clockdiv">
<span class="deadline_Text">You only have </span>
<span class="days"></span><span class="smalltext">Day</span>
<span class="hours"></span><span class="smalltext">Hours</span>
<span class="minutes"></span><span class="smalltext">Minutes</span>
<div class="clockLast">
<span class="seconds"></span><span class="smalltext">Seconds</span>
<span class="deadline_Text2">left to use your voucher code!</span>


And the javascript:

<script type="text/javascript">
var deadline = new Date(Date.UTC(2016,11, 22, 23, 59, 59));
function time_remaining(endtime){
var t = endtime - new Date();
var seconds = Math.floor( (t/1000) % 60 );
var minutes = Math.floor( (t/1000/60) % 60 );
var hours = Math.floor( (t/(1000*60*60)) % 24 );
var days = Math.floor( t/(1000*60*60*24) );
return {'total':t, 'days':days, 'hours':hours, 'minutes':minutes, 'seconds':seconds};
function run_clock(id,endtime){
var clock = document.getElementById(id);

var days_span = clock.querySelector('.days');
var hours_span = clock.querySelector('.hours');
var minutes_span = clock.querySelector('.minutes');
var seconds_span = clock.querySelector('.seconds');

function update_clock(){
var t = time_remaining(endtime);

days_span.innerHTML = t.days;
hours_span.innerHTML = ('0' + t.hours).slice(-2);
minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
seconds_span.innerHTML = ('0' + t.seconds).slice(-2);

if(<=0){ clearInterval(timeinterval); document.getElementById('deadline_Container').style.display = "none"; document.getElementById('deadline_Container2').style.display = "block"; }
var timeinterval = setInterval(update_clock,1000);

So it's supposed to be set to expire at 23.59 on the 22nd of November (2016, 11, 22).
Now it all work apart from when I check the countdown it says 30 days left. All the other values are right (hours, minutes, seconds).
Now if I set the date to 2016,10,22 it says 0 days, so then it's right. I guess this fixes the problem but I don't really understand why this is happening.
Can anybody see what's wrong with the code?

Answer Source

The month parameter is zero-based, so January = 0, November = 10 etc.

So, in your case if you want an end date of 22 Nov 2016 you need to set it as:

var deadline = new Date(Date.UTC(2016,10, 22, 23, 59, 59));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download