是否有任何方法我可以使用moment.js格式方法持续时间对象?我在文档中找不到它,也没有看到它是持续时间对象的属性。

我希望能够做到以下几点:

var diff = moment(end).unix() - moment(start).unix();
moment.duration(diff).format('hh:mm:ss')

此外,如果有其他库可以轻松地容纳这种功能,我会很有兴趣推荐。

谢谢!


当前回答

根据ni-ko-o-kin的回答:

meassurements = ["years", "months", "weeks", "days", "hours", "minutes", "seconds"];
withPadding = (duration) => {
    var step = null;
    return meassurements.map((m) => duration[m]()).filter((n,i,a) => {
        var nonEmpty = Boolean(n);
        if (nonEmpty || step || i >= a.length - 2) {
            step = true;
        }
        return step;
    }).map((n) => ('0' + n).slice(-2)).join(':')
}

duration1 = moment.duration(1, 'seconds');
duration2 = moment.duration(7200, 'seconds');
duration3 = moment.duration(604800, 'seconds');

withPadding(duration1); // 00:01
withPadding(duration2); // 02:00:00
withPadding(duration3); // 01:07:00:00:00

其他回答

不再需要(如果曾经有过的话)将持续时间转换为utc来解决这个问题。这就像将base10的“1”转换为二进制,然后说由于输出“1”看起来像base10,我们将没有任何问题,假设这是一个base10值,用于任何后续操作。

使用moment-duration-format,注意{trim: false}可以防止修剪:

moment.duration(1000000, "seconds").format("hh:mm:ss", { trim: false })
> "277:46:40"
moment.duration(0, "seconds").format("hh:mm:ss", { trim: false })
> "00:00:00"

让我们将其与不推荐的滥用utc的方法进行比较:

moment.utc(moment.duration(1000000, "seconds").asMilliseconds()).format('HH:mm:ss')
> "13:46:40"

如果你使用angular,添加这个到你的过滤器:

.filter('durationFormat', function () {
    return function (value) {
        var days = Math.floor(value/86400000);
        value = value%86400000;
        var hours = Math.floor(value/3600000);
        value = value%3600000;
        var minutes = Math.floor(value/60000);
        value = value%60000;
        var seconds = Math.floor(value/1000);
        return (days? days + ' days ': '') + (hours? hours + ' hours ': '') + (minutes? minutes + ' minutes ': '') + (seconds? seconds + ' seconds ': '')
    }
})

使用的例子

<div> {{diff | durationFormat}} </div>

Moment.duration (x).format()已弃用。 您可以使用moment.utc(4366589).format("HH:mm:ss")来获得所需的响应。

console.log (moment.utc (4366589) .format (HH: mm: ss)) < script src = " https://momentjs.com/downloads/moment.js " > < /脚本> < script src = " https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js " > < /脚本>

var diff = moment(end).unix() - moment(start).unix();
moment.utc(moment.duration(diff).asMilliseconds()).format("HH:mm:ss.SSS");

这可以用于将前两个字符作为小时,最后两个字符作为分钟。同样的逻辑也适用于秒。

/** * PT1H30M -> 0130 * @param {ISO String} isoString * @return {string} absolute 4 digit number HH:mm */ const parseIsoToAbsolute = (isoString) => { const durations = moment.duration(isoString).as('seconds'); const momentInSeconds = moment.duration(durations, 'seconds'); let hours = momentInSeconds.asHours().toString().length < 2 ? momentInSeconds.asHours().toString().padStart(2, '0') : momentInSeconds.asHours().toString(); if (!Number.isInteger(Number(hours))) hours = '0'+ Math.floor(hours); const minutes = momentInSeconds.minutes().toString().length < 2 ? momentInSeconds.minutes().toString().padEnd(2, '0') : momentInSeconds.minutes().toString(); const absolute = hours + minutes; return absolute; }; console.log(parseIsoToAbsolute('PT1H30M')); <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>