Moment.js:更改默认的humanize()行为 [英] Moment.js : change the default humanize() behavior

查看:337
本文介绍了Moment.js:更改默认的humanize()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在moment.js文档中给出了以下示例:

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize();  // a day

我需要打这个电话

moment.duration(1, "minutes").humanize();

输出"1分钟",并且我想要这个字符串

an output "1 minute" and I want to have for this string

moment.duration(24, "hours").humanize();

输出"24小时".

我不能只用1代替"a"一词,因为它将用于许多不同的语言.

I cannot just replace the "a" word with 1 because it is going to be used for many different languages.

有可能吗?也许一些简单的配置/解决方法/未记录的功能?

Is that possible? Maybe some simple config / workaround / non-documented feature?

推荐答案

来自 moment.js文档,您可以像这样自定义它:

From the moment.js documentation you can customize it like so:

moment.locale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "seconds",
        m:  "a minute",  //change that to "%d minute"
        mm: "%d minutes",
        h:  "an hour",   //here too
        hh: "%d hours",
        d:  "a day",     //and here
        dd: "%d days",
        M:  "a month",   //and so on...
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

我知道此解决方案并不完美,但找不到更好的解决方法.

I know this solution is not perfect, but couldn't find any better.

这篇关于Moment.js:更改默认的humanize()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆