Angular 2 “time ago” pipe

前端 未结 8 1442
余生分开走
余生分开走 2020-12-08 04:56

I am trying to create a \'time ago\' pipe for my Angular 2 application.

It should transform a date to a string such as \'5 minutes ago\' or \'60 seconds ago\'. It wo

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 05:00

    The following library does equivalent job in English and could be forked to change the language or support different ones:

    https://www.npmjs.com/package/time-ago-pipe

    npm install time-ago-pipe --save
    

    Then in the @NgModule you want to use it in:

    import {TimeAgoPipe} from 'time-ago-pipe'
    
    @NgModule({
        imports: [... etc ...],
        declarations: [AppComponent, ...etc..., TimeAgoPipe],
        bootstrap: [AppComponent]
    })
    

    And in the template:

    {{your_date | timeAgo}}
    

提交回复
热议问题