简体   繁体   English

如何使用MomentJS从特定时间开始计数

[英]How do I implement counting from a specific time using MomentJS

I'm using momentJS for real time (counting time) clock on my project. 我在我的项目中使用momentJS实时(计数时间)时钟。 And is working perfectly. 并且运行良好。 Here is my code: 这是我的代码:

interval = setInterval(function() {
    momentNow = moment();
    moment_time = momentNow.format('hh:mm:ss A');
    moment_date = momentNow.format('MMMM DD, YYYY') + ' ' 
        + momentNow.format('dddd')
        .substring(0,3).toUpperCase();
    // displays the time
    real_time.html(moment_time);
    real_date.html(moment_date);
}, 1000);

Like any human being, I wanted more from the plugin. 像任何人一样,我希望插件提供更多。 I want to display a counting time from a specific time rendered from the interval say momentNow = 02:22:30 PM . 我想显示一个从间隔说的特定时间开始的计数时间,例如momentNow = 02:22:30 PM

Basically, the logic is this, 基本上,逻辑是这样的

Pseudo code: 伪代码:

interval = setInterval(function() {
    var started = "02:22:30 PM";
    rendered_time.html(started);
}, 1000);

What you want is moment.diff(timestamp) 您想要的是moment.diff(timestamp)

Example code 范例程式码

timestamp = moment().now()
setInterval(function() {
  rendered_time.html(moment(moment().diff(timestamp)).format("hh:mm:ss A"));
});

http://momentjs.com/docs/#/displaying/difference/ http://momentjs.com/docs/#/displaying/difference/

您可以简单地使用from请参阅此处的文档 ):

moment([2007, 0, 28]).from([2007, 0, 27])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM