-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
59 lines (53 loc) · 2.52 KB
/
functions.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
let birthdayDate, userName, inputDateObject;
document.querySelector(".countdown").style.display = "none";
document.getElementById("start").addEventListener("click", function () {
const inputDate = document.getElementById("dob-input").value;
userName = document.getElementById("name").value;
if (!isValidDate(inputDate)) {
alert("Please enter a valid date (DD-MM-YYYY).");
return;
}
inputDateObject = new Date(inputDate + "T00:00:00");
const today = new Date();
inputDateObject.setFullYear(today.getFullYear());
birthdayDate = inputDateObject.getTime();
console.log("inputDateObject GOOD:", inputDateObject);
document.querySelector(".data").style.display = "none";
document.querySelector(".countdown").style.display = "block";
updateCountdown();
});
function updateCountdown() {
const countdownElement = document.getElementById("countdown");
if (!inputDateObject) {
console.error("Error: inputDateObject is undefined");
return;
}
const currentDate = new Date();
const timeLeft = birthdayDate - currentDate.getTime();
if (timeLeft <= 0) {
if (currentDate.getDate() === inputDateObject.getDate()) {
const birthdayGreeting = `<h2>Happy Birthday,${userName};PARTYYY!!!</h2>`;
countdownElement.innerHTML = birthdayGreeting;
}
else {
const belatedGreeting = `<h2>Happy Belated Birthday meet you again at your next birthday, ${userName}!</h2>`;
countdownElement.innerHTML = belatedGreeting;
}
} else {
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
document.getElementById("days").textContent = days.toString().padStart(2, '0');
document.getElementById("hours").textContent = hours.toString().padStart(2, '0');
document.getElementById("minutes").textContent = minutes.toString().padStart(2, '0');
document.getElementById("seconds").textContent = seconds.toString().padStart(2, '0');
setTimeout(updateCountdown, 1000);
}
}
function isValidDate(dateString) {
const regEx = /^\d{4}-\d{2}-\d{2}$/;
if (!dateString.match(regEx)) return false;
const d = new Date(dateString + "T00:00:00");
return !isNaN(d.getTime());
}