超过24小时的Moment JS格式失败

时间:2019-03-08 12:24:45

标签: javascript momentjs

AM试图在24小时内将momentjs格式化为hh:mm:ss,但我只得到1小时,所以我已经尝试过

 const start = moment('2019-02-27 00:00');
 const end = moment('2019-03-01 01:00');
 var diff =end.diff(start, 'seconds', true)

 let ff = moment.utc((diff) * 1000).format('HH:mm:ss')

 console.log(ff)

在上面,我希望得到

49:00:00

因为它是两天零一小时,所以24*2因为每天都有24小时加上从00:0到01:00的一小时

我在这里出错了,因为它显示的是01:00:00而不是49:00:00

1 个答案:

答案 0 :(得分:1)

也遇到同样的问题,如果您有帮助,请查看我的代码段。

let start = moment('2019-03-01 00:00:00');
let stop = moment();

const formatTimespan = (start, stop) => {
  const diff = stop.diff(start, "hours", true);

  const hours = Math.floor(diff);
  const step = 60 * (diff - hours)
  let mins = Math.floor(step)
  let secs = Math.floor(60 * (step - mins));
  mins = mins.toString().padStart(2, "0");
  secs = secs.toString().padStart(2, "0");
  return `${hours}:${mins}:${secs}`;  
}

console.log(formatTimespan(start, stop));