0

我正在尝试使用 moment.js 和 .format() 格式化时间 hh:mm:ss(音乐/播客持续时间):

[...]
let tkd = moment.duration(trackLenght);
let prg = moment.duration(trackProgress);
let fin = prg.format("hh:mm:ss") + ' / ' + tkd.format("hh:mm:ss");

此代码按预期工作,例如:02:40 / 05:55

但是当任何一个都低于 1 分钟时,mm:ss 格式变为32 / 05:55.

有没有办法在00:32 / 05:55不操纵字符串的情况下强制它?(使用 .format() 有效,但如果 trackLenght 低于 0 秒,则生成的字符串为360 miliseconds / 05:55)。谢谢

4

1 回答 1

0

从 moment-duration-format 的文档中,我发现有第二个参数可以传递给.format().

尝试使用该代码

let fin = `${prg.format('HH:mm:ss', { trim: false })} / ${tkd.format('HH:mm:ss', { trim: false })}`;

获取时间的正确字符串是 with HH

于 2021-09-02T10:01:46.910 回答