-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (113 loc) · 4.2 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
"use strict";
const arrowDiv = document.getElementById("down-arrow");
const resultDays = document.getElementById("result-days");
const resultMonths = document.getElementById("result-months");
const resultYears = document.getElementById("result-years");
arrowDiv.addEventListener("click", function () {
displayAge();
});
// Display error to user
function displayErrorMessage(field, errorMessage) {
document.getElementById(`${field}`).classList.add("empty-error");
document.getElementById(`${field}-descrip`).style.color = "hsl(0, 65%, 67%)";
document.getElementById(`${field}-error`).textContent = errorMessage;
}
// Remove error message
function removeErrorMessage(field) {
document.getElementById(`${field}`).classList.remove("empty-error");
document.getElementById(`${field}-descrip`).style.color = "hsl(0, 1%, 44%)";
document.getElementById(`${field}-error`).textContent = "";
}
// Display age in years, months, and days after submitting a valid date through the form
function displayAge() {
let day = document.getElementById("day").value;
let month = document.getElementById("month").value;
const year = document.getElementById("year").value;
// Receive validation errors if any field is empty when the form is submitted
if (!day || !month || !year) {
if (!day) {
displayErrorMessage("day", "This field is required");
} else {
removeErrorMessage("day");
}
if (!month) {
displayErrorMessage("month", "This field is required");
} else {
removeErrorMessage("month");
}
if (!year) {
displayErrorMessage("year", "This field is required");
} else {
removeErrorMessage("year");
}
} else {
removeErrorMessage("day");
removeErrorMessage("month");
removeErrorMessage("year");
}
// Display error if day, month or year input fields aren't within specified bounds
if (day !== "") {
if (day < 1 || day > 31) {
displayErrorMessage("day", "Must be a valid day");
}
}
if (month !== "") {
if (month < 1 || month > 12) {
displayErrorMessage("month", "Must be a valid month");
}
}
if (year !== "") {
if (year.length > 4 || year.length < 4 || year > 2023) {
displayErrorMessage("year", "Must be in the past");
}
}
// Check if date is valid
if (day !== "" && month !== "" && year !== "") {
if (0 < day < 32 && 0 < month < 13 && year <= 2023) {
// Check if user has entered a leading zero before modifying the day and month strings
day = Number(day) < 10 && day[0] !== "0" ? "0" + day : day;
month = Number(month) < 10 && month[0] !== "0" ? "0" + month : month;
const dateString = `${year}-${month}-${day}`;
const date = moment(dateString, "YYYY-MM-DD", true);
if (!date.isValid()) {
displayErrorMessage("day", "Must be a valid date");
displayErrorMessage("month", "");
displayErrorMessage("year", "");
// Display age
} else {
const birthDate = new Date(`${year}-${month}-${day}`); // Convert birthdate to Date object
const today = new Date(); // Date object representing current date and time
// Calculate age in years, months, and days
let ageYears = today.getFullYear() - birthDate.getFullYear();
let ageMonths = today.getMonth() - birthDate.getMonth();
let ageDays = today.getDate() - birthDate.getDate();
// Handle cases where birthday hasn't occurred in current year or current month
if (
ageMonths < 0 ||
(ageMonths === 0 && today.getDate() < birthDate.getDate())
) {
ageYears--;
ageMonths += 12;
}
// Handle case where birth date day value is greater than the current day
if (ageDays < 0) {
const prevMonthDate = new Date(
today.getFullYear(),
today.getMonth() - 1,
1
);
const prevMonthDays = new Date(
prevMonthDate.getFullYear(),
prevMonthDate.getMonth() + 1,
0
).getDate();
ageDays += prevMonthDays;
ageMonths--;
}
resultDays.textContent = ageDays;
resultMonths.textContent = ageMonths;
resultYears.textContent = ageYears;
}
}
}
}