-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (45 loc) · 1.28 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
function lpad(x, l, w) {
w = w || ' ';
var s = '' + x;
while (s.length < l) s = w + s;
return s;
}
function dateTweenFormatDate(d) {
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
return lpad(monthNames[d.getMonth()], 10) + ' ' + lpad(d.getDate(), 2) + ', ' + d.getFullYear();
}
function dateTweenFormatTime(d) {
var h = d.getHours() + 1;
var ampm = 'am';
if (h > 12) {
h -= 12;
ampm = 'pm';
}
return lpad(h, 2) + ':' + lpad(d.getMinutes(), 2, '0') +
':' + lpad(d.getSeconds(), 2, '0') + ampm;
}
function dateTween(a, b, t, c, f) {
var as = +a,
bs = +b,
dist = bs - as,
start = +new Date(),
r, end = start + t;
function update() {
var now = +new Date();
if (now < end) {
r = ((now - start) / t);
c(new Date(as + (dist * r)));
setTimeout(update, 10);
} else {
c(new Date(bs));
if (f) f(new Date(bs));
}
}
update();
}
if (typeof module !== 'undefined') module.exports = {
dateTween: dateTween,
dateTweenFormatDate: dateTweenFormatDate,
dateTweenFormatTime: dateTweenFormatTime
};