简体   繁体   English

格式化Moment.js持续时间

[英]Format Moment.js Duration

I am using Moment.js to track the duration of an action in my app. 我正在使用Moment.js跟踪我的应用中操作的持续时间。 Currently, I have the following code: 目前,我有以下代码:

var startMoment = null;

var actionClock = null;
function startClock() {
  actionClock = setInterval(function(){ 
    if (startMoment === null) {
      startMoment = moment();
      $('#duration').text('00:00:00');
    } else {
      var now = moment();
      var span = moment.duration(now - startMoment);
      $('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds());
    }
  }, 1000);     
}

function stopClock() {          
  clearInterval(actionClock);

      var now = moment();
      var span = moment.duration(now - startMoment);
      $('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds());

  startMoment = null;
  actionClock = null;
}

My problem is, the format of duration is awkward. 我的问题是,持续时间的格式很尴尬。 I want to display the duration as mm:ss.lll. 我想将持续时间显示为mm:ss.lll。 In other words, always show two digits for the minutes, two digits for the seconds, and three digits for the milliseconds. 换句话说,请始终在分钟上显示两位数字,在秒上显示两位数字,并在毫秒上显示三位数字。 Currently, I see durations printed like 1:2.45 . 目前,我看到的时长打印为1:2.45 How do I format a duration created with Moment.js? 如何格式化通过Moment.js创建的持续时间? If this is not possible, is there another library I should be using to track a duration and display it? 如果无法做到这一点,我应该使用另一个库来跟踪持续时间并显示它吗?

Thank you! 谢谢!

One way of doing this might be to convert your duration back into a moment (perhaps using milliseconds), and then using the moment's formatting. 一种执行此方法的方法可能是将持续时间转换回一个瞬间(也许使用毫秒),然后使用该瞬间的格式。

moment.utc(span.asMilliseconds()).format('mm:ss.SSS');

 var startMoment = null, $durForm = $('#durationFormatted'); var actionClock = null; function startClock() { actionClock = setInterval(function() { if (startMoment === null) { startMoment = moment(); $('#duration').text('00:00:00'); $durForm.text('00:00.000'); } else { var now = moment(); var span = moment.duration(now - startMoment); $('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds()); $durForm.text(moment(span.asMilliseconds()).format('mm:ss.SSS')); } }, 1000); } function stopClock() { clearInterval(actionClock); var now = moment(); var span = moment.duration(now - startMoment); $('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds()); $durForm.text(moment(span.asMilliseconds()).format('mm:ss.SSS')); startMoment = null; actionClock = null; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script> <div>Duration (original): <span id='duration'></span> </div> <div>Formatted moment: <span id='durationFormatted'></span> </div> <button onClick='startClock()'>Start Clock</button> <button onClick='stopClock()'>Stop Clock</button> 

I had a similar issue in my project and thankfully, Lodash was already a dependency. 我的项目中有一个类似的问题,幸运的是,Lodash已经是一个依赖项。 I was able to use the _.padStart method to get there. 我能够使用_.padStart方法到达那里。

let yourHours = _.padStart(span.hours().toString(),2,'0');
let yourMinute = _.padStart(span.minutes().toString(),2,'0');
let yourSeconds = _.padStart(span.seconds().toString(),2,'0');

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

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