blob: 1a8b7f1cbb290e2ef41bfe23e6ccfd84a0319de4 [file] [log] [blame]
Simon Hunt3ddb2e52014-12-12 13:59:54 -08001// ch09-01-time-ago.js
2
3angular.module('filterApp', [])
4 .filter('timeAgo', [function () {
5 var _m = 1000 * 60,
6 _h = _m * 60,
7 _d = _h * 24,
8 _mon = _d * 30;
9
10 return function (ts, ignoreSecs) {
11 var showSecs = !ignoreSecs,
12 now = new Date().getTime(),
13 diff = now - ts;
14
15 if (diff < _m && showSecs) {
16 return 'seconds ago';
17 } else if (diff < _h) {
18 return 'minutes ago';
19 } else if (diff < _d) {
20 return 'hours ago';
21 } else if (diff < _mon) {
22 return 'days ago';
23 } else {
24 return 'months ago';
25 }
26 }
27 }]);