-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (114 loc) · 4.31 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
document.addEventListener("DOMContentLoaded", function () {
// Get references to HTML elements
const inputDay = document.getElementById("inputDay");
const inputMonth = document.getElementById("inputMonth");
const inputYear = document.getElementById("inputYear");
const btn = document.querySelector(".btn");
const yearsSpan = document.querySelector(".years");
const monthsSpan = document.querySelector(".months");
const daysSpan = document.querySelector(".days");
const errorDay = document.getElementById("errorDay"); // Error message for day
const errorMonth = document.getElementById("errorMonth"); // Error message for month
const errorYear = document.getElementById("errorYear"); // Error message for year
btn.addEventListener("click", function (e) {
e.preventDefault();
// Clear previous error messages
clearErrorMessages();
// Get user input
const day = parseInt(inputDay.value);
const month = parseInt(inputMonth.value);
const year = parseInt(inputYear.value);
// Validate input
let isValid = true;
if (!isValidDate(day, month, year)) {
isValid = false;
if (day < 1 || day > 31) {
displayErrorMessage("Day must be between 1 and 31", errorDay);
}
if (month < 1 || month > 12) {
displayErrorMessage("Month must be between 1 and 12", errorMonth);
}
if (year < 1 || year > new Date().getFullYear()) {
displayErrorMessage(
`Year must be between 1 and ${new Date().getFullYear()}`,
errorYear
);
}
return;
}
// Calculate age
const currentDate = new Date();
const inputDate = new Date(year, month - 1, day); // Month is 0-based
const age = calculateAge(currentDate, inputDate);
// Display age with animation
animateAge(age);
});
function isValidDate(day, month, year) {
const currentDate = new Date();
const inputDate = new Date(year, month - 1, day); // Month is 0-based
// Check if the inputDate is a valid date and not in the future
return (
inputDate instanceof Date &&
!isNaN(inputDate) &&
inputDate <= currentDate &&
day >= 1 &&
day <= 31 &&
month >= 1 &&
month <= 12 &&
year >= 1 &&
year <= new Date().getFullYear()
);
}
function calculateAge(currentDate, inputDate) {
let age = {};
let yearDiff = currentDate.getFullYear() - inputDate.getFullYear();
let monthDiff = currentDate.getMonth() - inputDate.getMonth();
let dayDiff = currentDate.getDate() - inputDate.getDate();
if (dayDiff < 0) {
monthDiff--;
dayDiff += daysInMonth(currentDate.getMonth(), currentDate.getFullYear());
}
if (monthDiff < 0) {
yearDiff--;
monthDiff += 12;
}
age.years = yearDiff;
age.months = monthDiff;
age.days = dayDiff;
return age;
}
function daysInMonth(month, year) {
return new Date(year, month + 1, 0).getDate();
}
function animateAge(age) {
let years = 0;
let months = 0;
let days = 0;
const interval = 1; // Interval for each animation step (ms)
const intervalId = setInterval(function () {
if (years < age.years) {
years++;
yearsSpan.textContent = years;
} else if (months < age.months) {
months++;
monthsSpan.textContent = months;
} else if (days < age.days) {
days++;
daysSpan.textContent = days;
} else {
clearInterval(intervalId);
}
}, interval);
}
function displayErrorMessage(message, errorElement) {
errorElement.textContent = message;
errorElement.style.display = "block";
}
function clearErrorMessages() {
const errorMessages = document.querySelectorAll(".error-message");
errorMessages.forEach((error) => {
error.textContent = "";
error.style.display = "none";
});
}
});