bammab
bammab

Reputation: 2563

how to countdown to sunday automatically

I'm not sure exactly how to go about doing this, but I want to countdown to every Sunday. Then start the countdown back up again on Monday 12AM. I am using this code as my countdown but as of right now I have manually input the countdown date.

var end = new Date('15 Feb 2012 06:40:00');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {
            clearInterval(timer);
            alert('Expired');
            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = 'Days: ' + days + '<br />';
        document.getElementById('countdown').innerHTML += 'Hours: ' + hours + '<br />';
        document.getElementById('countdown').innerHTML += 'Minutes: ' + minutes + '<br />';
        document.getElementById('countdown').innerHTML += 'Seconds: ' + seconds + '<br />';
    }

    timer = setInterval(showRemaining, 1000);​

Upvotes: 0

Views: 744

Answers (1)

derekaug
derekaug

Reputation: 2145

You could always get the next Sunday like this:

var today = new Date(); 
var sunday = new Date(today.getFullYear(),today.getMonth(),today.getDate()+(7-today.getDay()));

Upvotes: 2

Related Questions