Search code examples
javascriptangularjsmomentjsangular-moment

Time ago in angular moment js


I am using Moment js and Angular Moment js to calculate time ago for my website. Everything is working perfectly except one thing. I don't know whether is it possible or not. I checked their humanize() documentation but didn't find anything usefull.

<span am-time-ago="messagedata.created | amFromUnix"></span>

Result: a day ago but i want 1 day ago. Is it possible? If yes how?


Solution

  • Those messages depend on the locale used by moment.js but you can update the locale to provide a different version of the messages

    moment.updateLocale('en', {
        relativeTime : {
            future: "in %s",
            past:   "%s ago",
            s:  "seconds",
            m:  "a minute",
            mm: "%d minutes",
            h:  "an hour",
            hh: "%d hours",
            d:  "a day",
            dd: "%d days",
            M:  "a month",
            MM: "%d months",
            y:  "a year",
            yy: "%d years"
        }
    });
    

    with the angular directive, you should be able to configure the moment instance when the application starts

    var myapp = angular.module('myapp', ['angularMoment']);
    myapp.run(function(amMoment) {
        amMoment.changeLocale(...);
    });
    

    you can check the moment.js documentation about customisation for more details