1.0.1 • Published 9 years ago

angular-filter-ago v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

angular-filter-ago

AngularJS filter that takes a date formatted YYYY-MM-DD HH24:MI:SS and returns a formatted string to the nearest whole time increment from seconds to weeks. After seven weeks, dates are displayed with an abbreviated month and year.

BEFOREAFTER
2015-10-20 13:40:02just now
2015-10-20 13:39:4913s ago
2015-10-20 12:59:0041m ago
2015-10-20 08:27:235h ago
2015-10-17 12:40:023d ago
2015-10-05 03:05:023w ago
2015-07-10 15:34:02Jul, 2015
2014-08-20 04:22:02Aug, 2014

Installation

1) Include the library in your index.html file:

<script type="text/javascript" src="./ago.js"></script>

2) Inject the module into your Angular app:

var app = angular.module('myApp', ['dds.ago']);

3) Use the filter:

{{ date | ago }}

Example

Assuming a current date and time of 2015-10-20 13:40:02 and the following JSON object:

{"item_list": [
    {"last_login":"2015-10-20 13:40:02", "user":"Merlin"},
    {"last_login":"2015-10-20 13:39:49", "user":"Donna"},
    {"last_login":"2015-10-20 12:59:00", "user":"Audrey"},
    {"last_login":"2015-10-20 08:27:23", "user":"Grant"},
    {"last_login":"2015-10-17 12:40:02", "user":"Samantha"},
    {"last_login":"2015-10-05 03:05:02", "user":"Vincent"},
    {"last_login":"2015-07-10 15:34:02", "user":"Pippy"},
    {"last_login":"2014-08-20 04:22:02", "user":"Roderick"}
]}

Using the following HTML snippit:

<table>
  <thead>
    <td>last_login</td>
    <td>user</td>
  </thead>
  <tbody>
    <tr ng-repeat="item in item_list">
      <td>{{ item.last_login }}</td>    
      <td>{{ item.last_login | ago }}</td>
      <td>{{ item.user }}</td>
    </tr>
  </tbody>
</table>

Will output:

last_loginlast_loginuser
2015-10-20 13:40:02just nowMerlin
2015-10-20 13:39:4913s agoDonna
2015-10-20 12:59:0041m agoAudrey
2015-10-20 08:27:235h agoGrant
2015-10-17 12:40:023d agoSamantha
2015-10-05 03:05:023w agoVincent
2015-07-10 15:34:02Jul, 2015Pippy
2014-08-20 04:22:02Aug, 2014Roderick