urish / angular-moment

Moment.JS directives for Angular.JS (timeago and more)
MIT License
2.6k stars 397 forks source link

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

Open Mr-Anonymous opened 8 years ago

Mr-Anonymous commented 8 years ago

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?

wembernard commented 8 years ago

:+1:

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

michaeljota commented 8 years ago

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

keemyb commented 8 years ago

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>