如何格式化'angular-moment''am-time-ago'指令?

时间:2014-04-09 13:52:59

标签: angularjs momentjs angular-moment

LIVE DEMO

我使用am-time-ago指令来显示相对时间戳:

<span am-time-ago="publishedAt"></span>

默认情况下,格式为“一天前”,“5天前”等等。

如何将格式更改为“1d”,“5d”,“3h”等?

2 个答案:

答案 0 :(得分:23)

您可以在配置或应用启动的某个位置自定义人性化。

moment.lang('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "%d seconds",
        m:  "1m",
        mm: "%dm",
        h:  "1h",
        hh: "%dh",
        d:  "1d",
        dd: "%dd",
        M:  "1m",
        MM: "%dm",
        y:  "1y",
        yy: "%dy"
    }
});

x = new moment();
z = x.clone().add('hours',1);
x.from(z, false);
>> 1h ago
x.from(z, true) //no ago
>> 1h

realtiveTime

上的文档

示例:http://jsbin.com/satohazu/1/edit

答案 1 :(得分:0)

Nix接受的答案有效,但现在已弃用。偶然发现此讨论的任何人(像我一样)都应使用moment.updateLocale(locale)

moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "%d seconds",
        m:  "1m",
        mm: "%dm",
        h:  "1h",
        hh: "%dh",
        d:  "1d",
        dd: "%dd",
        M:  "1m",
        MM: "%dm",
        y:  "1y",
        yy: "%dy"
    }
});

https://momentjs.com/docs/#/customization/relative-time/上的详细信息