-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (65 loc) · 2.51 KB
/
script.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
const years = document.getElementById("years");
const months = document.getElementById("months");
const days = document.getElementById("days");
const hours = document.getElementById("hours");
const minutes = document.querySelector("#minutes");
const seconds = document.querySelector("#seconds");
const loading = document.querySelector(".loading");
const countdown = document.querySelector("#countdown")
window.addEventListener("load", ()=>{
loading.style.display = "block";
setTimeout(()=>{
loading.style.display = "none";
countdown.style.display = "flex";
},1000)
})
let selectedBirthday = new Date();
let birthdayInput = document.querySelector("input[name=birthday]");
birthdayInput.addEventListener("change", (event) => {
selectedBirthday = new Date(event.target.value);
if (selectedBirthday > new Date()) {
alert("You cannot be born after this day!!")
return;
}
document.body.style.backgroundImage = "url('https://picsum.photos/id/175/800/400')";
loading.style.display = "block";
setInterval(updateCountdown, 1000);
setTimeout(() => {
loading.style.display = "none";
},1000)
});
const updateCountdown = () => {
let dobYear = selectedBirthday.getFullYear();
let dobMonth = selectedBirthday.getMonth();
let dobDay = selectedBirthday.getDate();
let dobHour = selectedBirthday.getHours();
let dobMinute = selectedBirthday.getMinutes();
let dobSecond = selectedBirthday.getSeconds();
let now = new Date();
let currentYear = now.getFullYear();
let currentMonth = now.getMonth();
let currentDay = now.getDate();
let currentHour = now.getHours();
let currentMinute = now.getMinutes();
let currentSecond = now.getSeconds();
let secondOfAge = currentSecond - dobSecond;
let minuteOfAge = currentMinute - dobMinute;
let hourOfAge = currentHour - dobHour;
let dayOfAge = currentDay - dobDay;
let monthOfAge = currentMonth - dobMonth;
let yearOfAge = currentYear - dobYear;
if (dayOfAge < 0) {
dayOfAge += 30;
monthOfAge --;
}
if (monthOfAge < 0) {
monthOfAge += 12;
yearOfAge--;
}
years.innerHTML = yearOfAge.toString().padStart(2,"0");
months.innerHTML = monthOfAge.toString().padStart(2,"0");
days.innerHTML = dayOfAge.toString().padStart(2,"0");
hours.innerHTML = hourOfAge.toString().padStart(2,"0");
minutes.innerHTML = minuteOfAge.toString().padStart(2,"0");
seconds.innerHTML = secondOfAge.toString().padStart(2,"0");
}