-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (84 loc) · 2.2 KB
/
index.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict'
/**
*
* Example usage is as :
* Difference between dates(Now and then) in seconds {{now|duration:then}}
* Format seconds to HH:MM:SS {{123|toHMS}}
* Difference between dates(Now and then) in HH:MM:SS: {{now|duration:then|toHMS}}
* Get latest/max date from unsorted array {{dateArray}}: {{dateArray|latest|date:"dd,MM,yyyy HH:mm:ss"}}
* Get oldest/min date from unsorted array {{dateArray}}: {{dateArray|oldest|date:"dd,MM,yyyy HH:mm:ss"}}
*
*/
angular
.module("timeShaper", [])
.filter("duration", function () {
return function (startTime, endTime) {
if (!startTime || !endTime) {
return 0;
}
if (typeof startTime == "string") {
startTime = new Date(startTime);
}
if (typeof endTime == "string") {
endTime = new Date(endTime);
}
return (endTime - startTime) / 1000;
};
})
.filter("latest", function () {
return function (dateArray) {
if (!dateArray.length) {
return "";
}
var max = "";
dateArray.forEach(function (date) {
if (date) {
var d = new Date(date);
if (max && d.valueOf() > max.valueOf()) {
max = d;
} else if (!max) {
max = d;
}
}
});
return max;
};
}).filter("oldest", function () {
return function (dateArray) {
if (!dateArray.length) {
return "";
}
var min = "";
dateArray.forEach(function (date) {
if (date) {
var d = new Date(date);
if (min && d.valueOf() < min.valueOf()) {
min = d;
} else if (!min) {
min = d;
}
}
});
return min;
};
})
.filter("toHMS", function () {
return function (secs) {
if (!secs || isNaN(secs)) {
secs = 0;
}
function z(n) {
return (n < 10 ? "0" : "") + n;
}
secs = Math.abs(Math.round(secs));
var sign = secs < 0 ? "-" : "";
return (
sign +
z((secs / 3600) | 0) +
":" +
z((secs % 3600 / 60) | 0) +
":" +
z(secs % 60)
);
};
});