-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (104 loc) · 3.78 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
// variable declaration
let years = 0;
let months = 0;
let days = 0;
let inputDate = '';
let currentDate = '';
const dayInput = document.querySelector('#day-input');
const monthInput = document.querySelector('#month-input');
const yearInput = document.querySelector('#year-input');
// animation and display related
function animateValue(obj, start, end, animationDuration) {
let startingTime = null;
const step = (currentTime) => {
if (!startingTime) startingTime = currentTime;
const progress = Math.min((currentTime - startingTime) / animationDuration, 1);
obj.textContent = Math.floor(progress * (end - start) + start);
if (progress < 1) {
window.requestAnimationFrame(step);
}
};
window.requestAnimationFrame(step);
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
async function displayResult() {
animateValue(document.querySelector('#no-of-years'), 0, years, 500);
await sleep(500);
animateValue(document.querySelector('#no-of-months'), 0, months, 500);
await sleep(500);
animateValue(document.querySelector('#no-of-days'), 0, days, 500);
}
// date calculation
function calcDate() {
const MS_PER_DAY = 1000 * 60 * 60 * 24;
const differenceDays = Math.floor((currentDate - inputDate) / MS_PER_DAY);
years = Math.floor(differenceDays / 365);
const daysAfterYears = differenceDays % 365;
months = Math.floor(daysAfterYears / 31);
days = daysAfterYears % 31;
displayResult();
}
// validation
function error(parentId, errorMessage) {
const children = document.querySelector(`#${parentId}`).children;
children[0].classList.add('error');
children[1].classList.add('error');
children[2].textContent = errorMessage;
}
function removeError(parentId) {
const children = document.querySelector(`#${parentId}`).children;
children[0].classList.remove('error');
children[1].classList.remove('error');
children[2].textContent = '';
}
function validateDate() {
currentDate = new Date();
inputDate = new Date(yearInput.value, monthInput.value - 1, dayInput.value);
let dayValid = false;
let monthValid = false;
let yearValid = false;
let allValid = false;
// validating day input
if (dayInput.value === '') {
error('days', 'This field is required');
} else if (dayInput.value < 1 || dayInput.value > 31 || isNaN(Number(dayInput.value))) {
error('days', 'Must be a valid day');
} else {
removeError('days');
dayValid = true;
}
// validating month input
if (monthInput.value === '') {
error('months', 'This field is required');
} else if (monthInput.value < 1 || monthInput.value > 12 || isNaN(Number(monthInput.value))) {
error('months', 'Must be a valid month');
} else {
removeError('months');
monthValid = true;
}
// validating year input
if (yearInput.value === '') {
error('years', 'This field is required');
} else if (yearInput.value > currentDate.getFullYear() || isNaN(Number(yearInput.value))) {
error('years', 'Must be in the past');
} else {
removeError('years');
yearValid = true;
}
// validating entire date after all inputs pass validation check
if (dayValid && monthValid && yearValid) {
if (inputDate.getMonth() != monthInput.value - 1 || inputDate > currentDate) {
error('days', 'Must be a valid date');
error('months', '');
error('years', '');
} else {
allValid = true;
}
}
// calculate date if no errors
if (allValid) {
calcDate();
}
}