-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
320 lines (271 loc) · 9.16 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Set the current date as the today value
let today = new Date();
// Set preset value on webpage load
window.onload = function () {
// Disabling future dates by setting max date to today
// document.getElementById("dob").max = today.toISOString().split("T")[0];
};
// To Validate the birth date
function validate(today, dob) {
// Log inputs for debugging
console.log(today, dob);
// Check for empty input
if (dob === null || dob === undefined) {
// Logs for debugging
console.log("Empty date : ", dob);
// Display the Error Message - Empty Date
document.getElementById("age").innerHTML =
"Date of Birth can not be empty !";
document.getElementById("age").classList.add("asterisk");
// Return false for invalid input
return false;
}
// Check for input date is today or not
if (today.toISOString().split("T")[0] === dob.toISOString().split("T")[0]) {
// Display the Message - Age as 0
document.getElementById("age").innerHTML =
'Age: 0 <span class="text-muted">(DOB same date as today)</span>';
// Return false to skip calculation
return false;
}
// Check for future date (input shouldn't be future date)
if (dob > today) {
// Logs for debugging
console.log("Inside Future validation...");
// Display the Error Message - Future Date
document.getElementById("age").innerHTML = "Future date is not allowed";
document.getElementById("age").classList.add("asterisk");
// Return false for invalid input
return false;
}
// This validates that the birth day entered does not exceed the maximum possible days for the given month and year combination
if (
new Date(dob.getFullYear(), dob.getMonth() + 1, 0).getDate() < dob.getDate()
) {
// Logs Invalid Date for debugging
console.log(
"Invalid Date : ",
new Date(dob.getFullYear(), dob.getMonth() + 1, 0),
dob.getDate()
);
// Display the Error Message - Date extends actual month max date
document.getElementById("age").innerHTML =
"Birth day entered can't exceed the maximum possible days in a month";
document.getElementById("age").classList.add("asterisk");
// Return false for invalid input
return false;
}
// Log today, date for debugging
console.log(
today.toISOString().split("T")[0],
dob.toISOString().split("T")[0]
);
// Return true for Valid input and continue calculation
return true;
}
// To calculate and display the age
function calculate(dob) {
// Validate input
if (!validate(today, dob)) return false;
// Get year, month and date value from date of birth
let dob_year = dob.getFullYear();
let dob_month = dob.getMonth() + 1; // month starts with 0
let dob_days = dob.getDate();
// Get current year, month and date value from today
let current_year = today.getFullYear();
let current_month = today.getMonth() + 1; // month starts with 0
let current_days = today.getDate();
// Initializing age variables (year, month and day)
let age_year = current_year - dob_year; // Calculate years
let age_month = 0;
let age_days = 0;
// Calculate months
if (dob_month > current_month) {
// Logs for debugging
console.log("Inside Month: DOB Month > Today.....");
// If birth month is greater than current month, it means that birthday has not passed yet this year
// So we calculate the months by taking current month + (12 - birth month)
age_month = current_month + (12 - dob_month);
// Subtracting 1 from the years since the birthday is still upcoming
age_year--;
} else {
// Logs for debugging
console.log("Inside Month: Current Month.....");
age_month = current_month - dob_month;
}
// Calculate days
if (dob_days > current_days) {
// Logs for debugging
console.log(
"Inside Day: DOB Date > Today.....",
new Date(dob_year, dob_month, 0).getDate(),
dob_days
);
// If birth date is greater than current date, the birthday has not passed yet
// So we calculate days by taking current date + (total days in birth month - birthday)
// Adding 1 to include the birth day in the count
age_days =
current_days +
(new Date(dob_year, dob_month, 0).getDate() - dob_days + 1); // new Date(year, month, 0) returns last day of month
age_month--; // Subtracting 1 from the month since the birthday is still upcoming
} else {
// Logs for debugging
console.log("Inside Day: Current Day.....");
age_days = current_days - dob_days;
}
// Set month as 11 if month goes to negative value
if (age_month < 0) {
// Logs for debugging
console.log("Inside Month is negative.....");
age_month = 11;
age_year--;
}
// Format age string
let age = `${age_year} years, ${age_month} month, and ${age_days} days old`;
// Display the Age
document.getElementById("age").innerHTML = "Age : " + age;
document.getElementById("age").classList.remove("asterisk");
return age;
}
// document.getElementById("age").innerHTML = "Age : " + calculate()
// * DatePicker
let calendar = document.querySelector(".calendar");
const month_names = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
isLeapYear = (year) => {
return (
(year % 4 === 0 && year % 100 !== 0 && year % 400 !== 0) ||
(year % 100 === 0 && year % 400 === 0)
);
};
getFebDays = (year) => {
return isLeapYear(year) ? 29 : 28;
};
generateCalendar = (month, year) => {
let calendar_days = calendar.querySelector(".calendar-days");
let calendar_header_year = calendar.querySelector("#year");
let days_of_month = [
31,
getFebDays(year),
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
calendar_days.innerHTML = "";
// Current/Today date
let currDate = new Date();
if (!month) month = currDate.getMonth();
if (!year) year = currDate.getFullYear();
let curr_month = `${month_names[month]}`;
month_picker.innerHTML = curr_month;
calendar_header_year.innerHTML = year;
// get first day of month
let first_day = new Date(year, month, 1);
for (let i = 0; i <= days_of_month[month] + first_day.getDay() - 1; i++) {
let day = document.createElement("div");
if (i >= first_day.getDay()) {
day.classList.add("calendar-day-hover");
day.innerHTML = i - first_day.getDay() + 1;
if (
i - first_day.getDay() + 1 === currDate.getDate() &&
year === currDate.getFullYear() &&
month === currDate.getMonth()
) {
day.classList.add("curr-date");
} else if (
i - first_day.getDay() + 1 >= currDate.getDate() &&
year >= currDate.getFullYear() &&
month >= currDate.getMonth()
) {
// Current month future date
day.classList.add("future-date");
} else if (currDate < first_day) {
// Future date and beyond
day.classList.add("future-date");
} else {
day.innerHTML += `<span></span>
<span></span>
<span></span>
<span></span>`;
}
}
day.onclick = () => {
// removing active date and id
calendar_days.childNodes.forEach((e) => {
e.classList.remove("bg-green");
e.removeAttribute("id", "select-day");
});
// applying active date and id
day.classList.add("bg-green");
day.setAttribute("id", "select-day");
// Get birth date from input
console.log("Starting Age Calculation...");
let dob = new Date(year, month, day.innerText);
dob.setHours(today.getHours());
dob.setMinutes(today.getMinutes());
dob.setSeconds(today.getSeconds());
console.log(dob, dob.toISOString());
// Calculate Age
calculate(dob);
};
calendar_days.appendChild(day);
}
};
let month_list = calendar.querySelector(".month-list");
month_names.forEach((e, index) => {
let month = document.createElement("div");
month.innerHTML = `<div data-month="${index}">${e}</div>`;
month.onclick = () => {
month_list.classList.remove("show");
curr_month.value = index;
generateCalendar(index, curr_year.value);
};
month_list.appendChild(month);
});
let month_picker = calendar.querySelector("#month-picker");
month_picker.onclick = () => {
month_list.classList.add("show");
};
let currDate = new Date();
let curr_month = { value: currDate.getMonth() };
let curr_year = { value: currDate.getFullYear() };
generateCalendar(curr_month.value, curr_year.value);
document.querySelector("#prev-year").onclick = () => {
--curr_year.value;
generateCalendar(curr_month.value, curr_year.value);
};
document.querySelector("#next-year").onclick = () => {
++curr_year.value;
generateCalendar(curr_month.value, curr_year.value);
};
// Reset the birthdate and Age value
function reset() {
// Re-generating calendar
curr_month = { value: currDate.getMonth() };
curr_year = { value: currDate.getFullYear() };
generateCalendar(curr_month.value, curr_year.value);
// Closing month selection area
month_list.classList.remove("show");
// Rest age display value and property
document.getElementById("age").innerHTML = "";
document.getElementById("age").classList.remove("asterisk");
}