angular-moment icon indicating copy to clipboard operation
angular-moment copied to clipboard

How can I show date difference like “Today, Yesterday, Tomorrow, etc..”?

Open Mr-Anonymous opened this issue 9 years ago • 3 comments

In my app, I have to show a to-do list where the lists are saved in date format like “2016-02-06” (YYYY-MM-DD) in sql. When I show those data in the angular front-end, I want the days to be listed as:

Today
Tomorrow,
2 Days from now,
etc..

I tried the following:

  1. am-time-ago directive: This is not what I want since it shows in hours ago for today, etc..

  2. amDifference filter: This sorta works but still today is listed as 0 and days like 'yesterday' cannot be listed. Also I would prefer to show tomorrow instead of 1 day from now.

Is there a way to achieve this with angular-moment?

Mr-Anonymous avatar Feb 06 '16 15:02 Mr-Anonymous

:+1:

I found http://momentjs.com/docs/#/displaying/tonow/ but wondering how to use it with angular-moment

wembernard avatar Feb 10 '16 17:02 wembernard

You can access the moment library directly and use that functions. I think.

michaeljota avatar Feb 17 '16 06:02 michaeljota

Hopefully this is of some use. I haven't used angular-moment to test it out though.

yourModule.filter('amD2', function() {
    var mappings = {
        "0 days": "Today",
        "1 day from now": "Tomorrow",
        "1 day ago": "Yesterday",
    };
    return function(amDifferenceOutput) {
        var mappedValue = mappings[amDifferenceOutput];

        return mappedValue || amDifferenceOutput;
    }
});

Usage:

<span ng-bind="myTime | amDifference | amD2"></span>

keemyb avatar Apr 21 '16 08:04 keemyb