0

this is my HTML code:

<ul class="countdown">
  <li class="timer"><div  id="days"></div><H1>Days</H1></li>
  <li class="timer"><div  id="hours"></div><H1>Hours</H1></li>
  <li class="timer"><div  id="mins"></div><H1>Minutes</H1></li>
  <li class="timer"><div  id="secs"></div><H1>Seconds</H1></li>
</ul>

And this is the JS code,

var xmas = new Date("jule 2, 2014 00:01:00");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
    clearTimeout(timer);
    document.write("Christmas is here!");
    // Run any code needed for countdown completion here
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById("days").innerHTML = days;
document.getElementById("hours").innerHTML = hours;
document.getElementById("mins").innerHTML = minutes;
document.getElementById("secs").innerHTML = seconds;
var timer = setTimeout('cdtd()',1000);

The result is working in Chrome but IE and Firefox return NaN.

kviiri
  • 3,282
  • 1
  • 21
  • 30
nodar
  • 3
  • 3
  • Here is an example: http://jsfiddle.net/SrFJ4/ – nodar May 24 '14 at 13:31
  • I think you may want to look at: http://stackoverflow.com/questions/3257460/new-date-is-working-in-chrome-but-not-firefox. – Winestone May 24 '14 at 13:46
  • NaN stands for "Not a Number" so somewhere you are trying to do something you can only do with numbers, yet the thing you are applying it to is not a number. Just a little fun fact :) – tomysshadow May 25 '14 at 00:06

2 Answers2

1

I got it working.

Live example at: http://testnaman.neocities.org/quicktest2.html. This will not be permanent.

You need to change:

var xmas = new Date("jule 2, 2014 00:01:00");

To:

var xmas = new Date("July 2, 2014 00:01:00");

As it seems Firefox doesn't like incorrect spelling.

For more information take a look at: new Date() is working in Chrome but not Firefox.

Community
  • 1
  • 1
Winestone
  • 1,500
  • 9
  • 19
1

Basically, your problem is how you set the date.

Should be like this:

var xmas = new Date("July 2, 2014 00:01:00");

Note "July" instead of "jule".

Have a look to new Date constructor and how Date format should be: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Date must be recognize by Date.parse()

Firefox implementation doesn't recognize that format, but chrome does.

avcajaraville
  • 9,041
  • 2
  • 28
  • 37