Question

Is it possible to define my time to countdown as hours now I have "December 25, 2014 00:01:00" but could I have something like this "14:00" and it runs every day. This is the code I have.

<!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function cdtd() {
        var xmas = new Date("December 25, 2014 00:01:00");
        var now = new Date();
        var timeDiff = xmas.getTime() - now.getTime();
        if (timeDiff <= 0) {
            clearTimeout(timer);
            document.write("Christmas is 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("daysBox").innerHTML = days;
        document.getElementById("hoursBox").innerHTML = hours;
        document.getElementById("minsBox").innerHTML = minutes;
        document.getElementById("secsBox").innerHTML = seconds;
        var timer = setTimeout('cdtd()',1000);
    }
    </script>
    </head>
    <body>
    Days Remaining:
    <div id="daysBox"></div>
    Hours Remaining:
    <div id="hoursBox"></div>
    Minutes Remaining:
    <div id="minsBox"></div>
    Seconds Remaining:
    <div id="secsBox"></div>
    <script type="text/javascript">cdtd();</script>
    </body>
    </html>

EDIT

Countdown works fine, but to make it work I have to define time (mm:dd:yy:hh:mm:ss). My question is can I define time like this(hh:mm:ss) because the date doesn't matter, only thing that matters is the time(hh:mm:ss) and when it comes to end countdown restarts and start counting again to example 14:25:00(hh:mm:ss).

Was it helpful?

Solution

Some working result in this fiddle: http://jsfiddle.net/dehisok/6Wu9a/1/

var now = new Date();
var xmas = new Date(now.getFullYear(),now.getMonth(),now.getDate(),23,1,0);

But there is a bug: if needle time is in past - it crashes :( Unfortunatelly, have no more time to fix.

I have changed document.write to jquery, because d.write isn't supported by jsfiddle.

I hope, it's what you need.

Good luck!

OTHER TIPS

Don't leave parsing date strings to the date constructor. ES5 defines a string format to be supported, but not all browsers support it. Prior to ES5, parsing of date strings was entirely implementation dependent. In this case, you know exactly the date and time you wish to set so use numeric arguments:

>    var xmas = new Date(2014, 11, 25);

which will create a Date object for 2014-12-25T00:00:00 in the local time zone based on system settings.

>    if (timeDiff <= 0) {
>        clearTimeout(timer);

There is no need to clear any timeout, just don't call setTimeout again.

>        document.write("Christmas is here!");

If called after the load event, that will clear the entire document. Probably not what you want to do for someone who has been watching the timer up to midnight, then loses the entire page. :-)

>    var timer = setTimeout('cdtd()',1000);

Calling setTimeout every 1 second means that the timer will gradually drift and occassionally appear to skip a second. It will also not "tick" consistently if compared to the system clock. Instead, set the delay based on the current milliseconds, e.g.

>    var timer = setTimeout('cdtd()', (1020 - now%1000));

so it next runs about 20ms after the next full second. And if the time has expired, just don't call setTimeout.

If you are just looking format the time in hours like hh:mm:ss then you need a small function to padd single digit numbers and then concatenate the values, e.g.

function pad(n){return (n<10? '0' ; '') + n;}

then:

document.getElementById("hoursBox").innerHTML = pad(hours) + ':' + pad(minutes) + ':' +
                                                pad(seconds);

You can work out how to format the date part from there. Note that month:day:year format is very confusing to the vast majority of web users. Far better to use an ISO 8601 format like year-month-day hh:mm:ss

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top