-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
105 lines (90 loc) · 2.37 KB
/
app.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
// our months and days arrays
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const weekdays = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const giveaway = document.querySelector(".giveaway")
const deadline = document.querySelector(".deadline")
const items = document.querySelectorAll(".deadline-format h4")
// console.log(items)
let tempDate = new Date()
let tempYear = tempDate.getFullYear()
let tempMonth = tempDate.getMonth()
let tempDay = tempDate.getDate()
// Hard coded time
// let futureDate = new Date(2022, 4, 21, 6, 30, 0)
// console.log(futureDate)
const futureDate = new Date(tempYear, tempMonth, tempDay + 10, 11, 30, 0)
const year = futureDate.getFullYear()
const hours = futureDate.getHours()
const minutes = futureDate.getMinutes()
let month = futureDate.getMonth()
// console.log(months[month])
month = months[month]
const date = futureDate.getDate()
const weekday = weekdays[futureDate.getDay()]
// console.log(weekday)
giveaway.textContent = `giveaway ends on ${weekday}, ${date} ${month} ${year} ${hours}:${minutes}am`
// future time in ms
const futureTime = futureDate.getTime()
// console.log(futureTime)
function getRemainingTime() {
const today = new Date().getTime()
// console.log(today)
const t = futureTime - today
// console.log(t)
// 1s = 1000ms
// 1m = 60s
// 1hr = 60mins
// 1d = 24hrs
// values in ms
const oneDay = 24 * 60 * 60 * 1000
const oneHour = 60 * 60 * 1000
const oneMinute = 60 * 1000
// calculate all values:
let days = t / oneDay
// console.log(days)
days = Math.floor(days)
let hours = Math.floor((t % oneDay) / oneHour)
// console.log(9 % 2)
let minutes = Math.floor((t % oneHour) / oneMinute)
let seconds = Math.floor((t % oneMinute) / 1000)
// console.log(hours)
// set values array
const values = [days, hours, minutes, seconds]
function format(item) {
if (item < 10) {
return item = `0${item}`
}
return item
}
items.forEach(function (item, index) {
item.innerHTML = format(values[index])
})
if (t < 0) {
clearInterval(countdown)
deadline.innerHTML = `<h4 class="expired">sorry, this giveaway has expired</h4>`
}
}
// countdown
let countdown = setInterval(getRemainingTime, 1000)
getRemainingTime()