-
Notifications
You must be signed in to change notification settings - Fork 5
/
countdown.js
112 lines (85 loc) · 2.65 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function pad(n, digits) {
n = n.toString();
numZeroes = digits - n.length;
for (i = 0; i < numZeroes; i++) {
n = '0' + n;
}
return n;
}
function GetTimeAsString(currentTime) {
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var ampm = "AM";
if (hours > 11) {
ampm = "PM";
hours -= 12;
} else if (hours == 0) {
hours = 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
output = month + "/" + day + "/" + year + " " + hours + ":" + minutes + ":"
+ seconds + " " + ampm;
return output;
}
function UpdateCurrentTime() {
dateNow = new Date();
var timeString = GetTimeAsString(dateNow);
delete dateNow;
document.getElementById("currenttime").innerHTML = "<b>Current Time</b>: "
+ timeString;
setTimeout(UpdateCurrentTime, 1000);
}
function Countdown(eventName, endYear, endMonth, endDay, endHour, endMinute,
endSecond, divID){
// Subtract one from month to be zero-indexed
endMonth = endMonth - 1;
dateFuture = new Date(endYear,endMonth,endDay,endHour,endMinute,endSecond);
//grab current date
dateNow = new Date();
//calc milliseconds between dates
amount = dateFuture.getTime() - dateNow.getTime();
days=0;
hours=0;
mins=0;
secs=0;
if (amount >= 0) {
amount = Math.floor(amount/1000);
days=Math.floor(amount/86400);
amount=amount%86400;
hours=Math.floor(amount/3600);
amount=amount%3600;
mins=Math.floor(amount/60);
amount=amount%60;
secs=Math.floor(amount);
countdownTimeString = pad(days, 2) + ":" + pad(hours, 2) + ":" +
pad(mins, 2) + ":" + pad(secs, 2);
var daysStr;
if( days == 0 ) daysStr = "";
else daysStr = days+"d "
var hoursStr;
if( hours == 0 ) hoursStr = "";
else hoursStr = hours+"h "
var minsStr;
if( mins == 0 ) minsStr = "";
else minsStr = mins+"m "
var secsStr = secs+"s"
out = "<i>Coming soon ("+eventName+" ";
out += daysStr +" " + hoursStr + " " + minsStr + " " + secsStr+")</i>";
document.getElementById(divID).innerHTML=out;
endMonth = endMonth + 1;
setTimeout(function() {UpdateCurrentTime(); Countdown(
eventName, endYear, endMonth, endDay, endHour, endMinute, endSecond,
divID)}, 1000);
}
delete dateNow;
delete dateFuture;
}