Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countdown with moment.js

I'm trying to make a countdown of seconds between the current time since next Monday 00:00 with moment.js.

I have looked at the documentation (http://momentjs.com/docs/#/displaying/difference/) but I only arrived to have the days since next Monday, this is the code I try to make work to have the countdown. Any idea what I'm doing wrong?

var datetime = $('.datetime'),

//Update countdown
update = function(){
        var now = moment();
            nextmonday = moment().day(1),
            diff = now.diff(nextmonday, 'seconds'),
            hours = Math.floor(diff / 3600),
            diff = diff - hours * 3600,
            minutes = Math.floor(diff / 60),
            seconds = diff - minutes * 60;

        datetime.html(diff);

    };

//If it's Monday do nothing
if (moment().day()!=1){
    update();
    setInterval(update, 1000);
}
like image 526
Adrián Pérez Avatar asked Aug 15 '26 15:08

Adrián Pérez


1 Answers

  1. You have a typo on the line var now = moment();. Replace ; with ,
  2. You are defining diff twice, change the first one to something else like diffSec
  3. You are probably trying to get the difference to the begging of the day, next monday. Do this with nextmonday = moment().day(1).startOf('day').add('days',7)

Working example: http://jsfiddle.net/jondlm/x4MM6/

like image 113
Jondlm Avatar answered Aug 17 '26 06:08

Jondlm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!