用moment.js倒计时

时间:2014-01-04 20:59:55

标签: javascript time momentjs

我正在尝试从下周一00:00到moment.js之间的当前时间倒数秒。

我查看了文档(http://momentjs.com/docs/#/displaying/difference/),但我只是到了下周一以来的日子,这是我尝试进行倒计时工作的代码。知道我做错了什么吗?

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);
}

1 个答案:

答案 0 :(得分:2)

  1. var now = moment();上有拼写错误。将;替换为,
  2. 您要定义diff两次,将第一个更改为其他内容,例如diffSec
  3. 你可能正试图在下一个星期一与当天的乞讨有所不同。使用nextmonday = moment().day(1).startOf('day').add('days',7)
  4. 执行此操作

    工作示例:http://jsfiddle.net/jondlm/x4MM6/