In any case, this script displays Auction is over. How to fix?

<div id="clock1"></div> <script language="JavaScript"> StartCountDown("clock1","20/12/2016 16:00 PM -0400") function StartCountDown(myDiv,myTargetDate) { var dthen = new Date(myTargetDate); var dnow = new Date(); ddiff = new Date(dthen-dnow); gsecs = Math.floor(ddiff.valueOf()/1000); CountBack(myDiv,gsecs); } function Calcage(secs, num1, num2) { s = ((Math.floor(secs/num1))%num2).toString(); if (s.length < 2) { s = "0" + s; } return (s); } function CountBack(myDiv, secs) { var DisplayStr; var DisplayFormat = "%%D%% дней %%H%% часов %%M%% минут %%S%% секунд"; DisplayStr = DisplayFormat.replace(/%%D%%/g, Calcage(secs,86400,100000)); DisplayStr = DisplayStr.replace(/%%H%%/g, Calcage(secs,3600,24)); DisplayStr = DisplayStr.replace(/%%M%%/g, Calcage(secs,60,60)); DisplayStr = DisplayStr.replace(/%%S%%/g, Calcage(secs,1,60)); if(secs > 0) { document.getElementById(myDiv).innerHTML = DisplayStr; setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 990); } else { document.getElementById(myDiv).innerHTML = "Auction Over"; } } </script> 

Closed due to the fact that off-topic participants Dmitriy Simushev , Suvitruf , tutankhamun , Abyx , Mike 21 Dec '15 at 11:31 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, Suvitruf, tutankhamun, Abyx, Mike
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • new Date("20/12/2016 16:00 PM -0400") == Invalid Date - Grundy

1 answer 1

The whole problem is the wrong date format. When calling a function

 StartCountDown("clock1","20/12/2016 16:00 PM -0400") 

the value myTargetDate comes to the value "20/12/2016 16:00 PM -0400" and when executed var dthen = new Date(myTargetDate); dthen variable is dthen to "Inavlid Date" .

Further, all operations in which this variable participates return NaN .

And since all NaN comparison operations return false in the condition

 if(secs > 0) 

the else branch is always selected

 document.getElementById(myDiv).innerHTML = "Auction Over"; 

More information about the supported formats can be found in the help: Date