-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
51 lines (43 loc) · 1.32 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
"use strict";
module.exports = function datetimeDiff (from, to) {
const min = [-Infinity, 1, 1, 0, 0, 0, 0];
const max = [Infinity, 12, null, 24, 60, 60, 1000];
if (to < from) {
const temp = to;
to = from;
from = temp;
}
let start = [from.getUTCFullYear(), from.getUTCMonth() + 1, from.getUTCDate(), from.getUTCHours(),
from.getUTCMinutes(), from.getUTCSeconds(), from.getUTCMilliseconds()],
end = [to.getUTCFullYear(), to.getUTCMonth() + 1, to.getUTCDate(), to.getUTCHours(), to.getUTCMinutes(),
to.getUTCSeconds(), to.getUTCMilliseconds()],
i = 7;
const dec = (i) => {
--end[i];
while (end[i] < min[i]) {
const r = dec(i - 1);
end[i] += max[i] === null
? r
: max[i];
}
return i === 1 ? new Date(Date.UTC(end[0], end[1], 0)).getUTCDate() : max[i + 1];
};
while (i > 0) {
--i;
let diff = end[i] - start[i];
while (diff < 0) {
end[i] += dec(i - 1);
diff = end[i] - start[i];
}
end[i] = diff;
}
return {
years: end[0],
months: end[1],
days: end[2],
hours: end[3],
minutes: end[4],
seconds: end[5],
milliseconds: end[6]
};
};