How to format 'angular-moment's 'am-time-ago' directive?

前端 未结 2 1679
名媛妹妹
名媛妹妹 2020-12-31 19:18

LIVE DEMO

I use the am-time-ago directive to show a relative timestamp:


By de

相关标签:
2条回答
  • 2020-12-31 19:24

    You could customize humanize, somewhere in your config or app start.

    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
    

    Docs on realtiveTime

    Example: http://jsbin.com/satohazu/1/edit

    0 讨论(0)
  • 2020-12-31 19:43

    The accepted answer by Nix works, but is deprecated by now. Anybody who stumbles across this discussion (like I did) should use 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"
        }
    });
    

    Details at https://momentjs.com/docs/#/customization/relative-time/

    0 讨论(0)
提交回复
热议问题