在moment.js中完全人性化的持续时间

hus*_*ayt 47 momentjs

我在moment.js尝试过这个

moment.duration(375,'days').humanize()
Run Code Online (Sandbox Code Playgroud)

得到"一年"的答案,但我希望"一年零十天".在moment.js中有没有办法获得完整的人性化价值?

Nic*_*s C 19

我发现这个小lib,只显示持续时间(如果你真的不需要moment.js的所有功能)

https://github.com/EvanHahn/HumanizeDuration.js


Kis*_*ngi 19

Moment.js提供了fromNow在人类可读的时间内获取持续时间的功能,请参阅http://momentjs.com/docs/#/displaying/fromnow/

例:

moment([2007, 0, 29]).fromNow(); // 4 years ago
moment().subtract(375, 'days').fromNow(); // a year ago
Run Code Online (Sandbox Code Playgroud)

您需要使用@Fluffy建议的第三方库

  • 不需要第三者。您可以使用 [`moment.relativeTimeThreshold('y', 365)`](https://github.com/moment/momentjs.com/blob/master/docs/moment/07-customization/13-relative-time- threshold.md) 来设置舍入。 (4认同)

小智 10

我正在看同样的问题,似乎没有计划在未来支持这个...

虽然提出的一种解决方法是创建一个语言定义来覆盖人性化消息的默认实现:

https://github.com/timrwood/moment/issues/348

如果你问我,有点矫枉过正......


Flu*_*ffy 10

试试这个插件:

https://github.com/jsmreese/moment-duration-format

moment.duration(123, "minutes").format("h [hrs], m [min]");
// "2 hrs, 3 min"
Run Code Online (Sandbox Code Playgroud)


RJF*_*ner 7

使用moment.relativeTimeThreshold('y', 365)设置四舍五入。

moment.relativeTimeThreshold('s', 60);
moment.relativeTimeThreshold('m', 60);
moment.relativeTimeThreshold('h', 24);
moment.relativeTimeThreshold('d', 31);
moment.relativeTimeThreshold('M', 12);
moment.relativeTimeThreshold('y', 365);
Run Code Online (Sandbox Code Playgroud)


Syn*_*cro 5

我做了一个函数来解决这个问题。

function formatDuration(period) {
    let parts = [];
    const duration = moment.duration(period);

    // return nothing when the duration is falsy or not correctly parsed (P0D)
    if(!duration || duration.toISOString() === "P0D") return;

    if(duration.years() >= 1) {
        const years = Math.floor(duration.years());
        parts.push(years+" "+(years > 1 ? "years" : "year"));
    }

    if(duration.months() >= 1) {
        const months = Math.floor(duration.months());
        parts.push(months+" "+(months > 1 ? "months" : "month"));
    }

    if(duration.days() >= 1) {
        const days = Math.floor(duration.days());
        parts.push(days+" "+(days > 1 ? "days" : "day"));
    }

    if(duration.hours() >= 1) {
        const hours = Math.floor(duration.hours());
        parts.push(hours+" "+(hours > 1 ? "hours" : "hour"));
    }

    if(duration.minutes() >= 1) {
        const minutes = Math.floor(duration.minutes());
        parts.push(minutes+" "+(minutes > 1 ? "minutes" : "minute"));
    }

    if(duration.seconds() >= 1) {
        const seconds = Math.floor(duration.seconds());
        parts.push(seconds+" "+(seconds > 1 ? "seconds" : "second"));
    }

    return "in "+parts.join(", ");
}
Run Code Online (Sandbox Code Playgroud)

该函数采用句点字符串 (ISO 8601),使用 Moment (>2.3.0) 对其进行解析,然后对于每个时间单位,将一个字符串推送到数组中parts。然后parts数组内的所有内容都作为分隔字符串连接在一起", "

您可以在这里测试它: https: //jsfiddle.net/mvcha2xp/6/

我将它用作 Vue 过滤器,以正确地人性化持续时间。

  • 很高兴分享你的解决方法伙伴,但这对于国际化来说太糟糕了,因为在某些语言中,显示顺序可能会改变......总比没有好,是的,但我不能按原样使用它;我将尝试看看使用矩方法是否容易将其国际化。 (2认同)