-
Notifications
You must be signed in to change notification settings - Fork 4
/
countdown.js
60 lines (55 loc) · 1.62 KB
/
countdown.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
function pluralString(n, base)
{
if (n == 0) return "";
else if (n == 1) return '1\xa0' + base + ', ';
else return n + '\xa0' + base + 's, ';
}
function deltaToString(distance)
{
if (isNaN(distance))
{
return "???";
}
distance /= 1000; // from milliseconds
var seconds = Math.floor(distance % 60);
distance -= seconds;
distance /= 60;
var minutes = Math.floor(distance % 60);
distance = (distance - minutes) / 60;
var hours = Math.floor(distance % 24);
distance = (distance - hours) / 24;
var days = Math.floor(distance % 365);
distance = (distance - days) / 365;
var years = Math.floor(distance);
var s = pluralString(years, 'year') + pluralString(days, 'day');
s += pluralString(hours, 'hour') + pluralString(minutes, 'minute');
return s + seconds + "\xa0seconds";
}
function updateDict()
{
var now = new Date().getTime();
for (key in timing)
{
var entry = timing[key];
if (!entry["start_t"]) entry["start_t"] = new Date(entry["start"]).getTime();
if (!entry["end_t"]) entry["end_t"] = new Date(entry["end"]).getTime();
if (!entry["lifespan"]) entry["lifespan"] = entry["end_t"] - entry["start_t"];
var start = new Date(timing[key]["start"]);
if (entry["end_t"] < now)
{
entry["status"] = "expired";
continue;
}
else if (now < entry["start_t"])
{
entry["status"] = "future";
entry["release"] = entry["start_t"] - now;
}
else
{
entry["status"] = "active";
entry["age"] = now - entry["start_t"];
}
entry["delta"] = entry["end_t"] - now;
}
}