summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/js/ch09-01-time-ago.js
blob: 1a8b7f1cbb290e2ef41bfe23e6ccfd84a0319de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ch09-01-time-ago.js

angular.module('filterApp', [])
    .filter('timeAgo', [function () {
        var _m = 1000 * 60,
            _h = _m * 60,
            _d = _h * 24,
            _mon = _d * 30;

        return function (ts, ignoreSecs) {
            var showSecs = !ignoreSecs,
                now = new Date().getTime(),
                diff = now - ts;

            if (diff < _m && showSecs) {
                return 'seconds ago';
            } else if (diff < _h) {
                return 'minutes ago';
            } else if (diff < _d) {
                return 'hours ago';
            } else if (diff < _mon) {
                return 'days ago';
            } else {
                return 'months ago';
            }
        }
    }]);