forked from john-smilga/javascript-basic-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
89 lines (77 loc) · 2.14 KB
/
app.js
File metadata and controls
89 lines (77 loc) · 2.14 KB
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
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')
let tempDate = new Date();
let tempYear = tempDate.getFullYear();
let tempMonth = tempDate.getMonth();
let tempDay = tempDate.getDate();
// months are ZERO index based;
const futureDate = new Date(tempYear, tempMonth, tempDay + 10, 11, 30, 0);
// const futureDate=new Date(2021, 0, 15, 10, 5, 0)
const year=futureDate.getFullYear()
let month=futureDate.getMonth()
month=months[month]
const day=futureDate.getDate()
const weekday=weekdays[futureDate.getDay()]
const hours=futureDate.getHours()
const minutes=futureDate.getMinutes()
giveaway.textContent=`giveaway end on ${weekday}, ${day} ${month} ${year}, ${hours}:${minutes < 10 ? '0' : ''}${minutes}am`
function getRemainingTime(){
const today=new Date().getTime()
const t= futureDate- today
// 1s = 1000ms
// 1m = 60s
// 1h = 60m
// 1d = 24h
// values in ms
const oneDay = 24*60*60*1000
const oneHour = 60*60*1000
const oneMinutes = 60*1000
//calculate all values
let days = Math.floor(t / oneDay)
let hours = Math.floor((t % oneDay) / oneHour)
let minutes = Math.floor((t % oneHour) / oneMinutes)
let seconds = Math.floor((t % oneMinutes) / 1000)
// set values to array
const values=[days, hours, minutes, seconds]
function format(item){
if(item < 10){
return (item = `0${item}`)
}
return item
}
items.forEach((item, index)=>{
item.innerHTML=format(values[index])
})
document.title=`${values[0]}Days, ${values[1]}Hours, ${values[2]}Minutes, ${values[3]}Seconds`
if(t < 0){
clearInterval(coundown)
deadline.innerHTML=`<h4 class="expired"> Sorry this giveaway has expired </h4>`
document.title="Sorry this giveaway has expired"
}
}
let coundown=setInterval(getRemainingTime, 1000)
getRemainingTime()