-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
164 lines (116 loc) · 4.09 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
// Select the form and add event
const form = document.querySelector('#formageApp');
form.addEventListener('submit', function(event) {
event.preventDefault(); // previene la acción por defecto del formulario
// Select fields input date
const dayInput = document.querySelector('#txt-day');
const monthInput = document.querySelector('#txt-month');
const yearInput = document.querySelector('#txt-year');
// Create msg Error
const dayError = document.querySelector('#error-day');
const monthError = document.querySelector('#error-month');
const yearError = document.querySelector('#error-year');
dayError.textContent = '';
monthError.textContent = '';
yearError.textContent = '';
// Get input value
const day = parseInt(dayInput.value);
const month = parseInt(monthInput.value);
const year = parseInt(yearInput.value);
// validate inputs
if(isNaN(day) || isNaN(month) || isNaN(year)){
dayError.textContent = 'Ths field is required';
monthError.textContent = 'This field is required';
yearError.textContent = 'This field is required';
msgError();
}
if (day < 1 || day > 31) {
dayError.textContent = 'Must be a valid day';
msgError();
}
if ( month < 1 || month > 12) {
monthError.textContent = 'Must be a valid month';
msgError();
}
if (year < 0 || year > new Date().getFullYear()) {
yearError.textContent = 'Must be in the past';
msgError();
}
if (!isNaN(day) && !isNaN(month) && !isNaN(year)) {
const date = new Date(year, month - 1, day);
if (date.getMonth() !== month - 1 || date.getDate() !== day) {
dayError.textContent = 'Must be a valid date';
msgError();
} else if (date > new Date()) {
yearError.textContent = 'Must be in the past';
msgError();
}else {
outputDate(year, month, day);
restoreStyle();
}
}
});
// Function manage the errors
function msgError() {
const lblRed = document.getElementsByClassName('lbl-label');
const textRed = document.getElementsByClassName('txt-text');
for(let i = 0; i < lblRed.length; i++){
lblRed[i].style.color = "hsl(0, 100%, 67%)";
}
for(let i = 0; i < textRed.length; i++){
textRed[i].style.borderColor = "hsl(0, 100%, 67%)";
}
reseter();
}
//functions reset
function restoreStyle() {
const lblRed = document.getElementsByClassName('lbl-label');
const textRed = document.getElementsByClassName('txt-text');
for(let i = 0; i < lblRed.length; i++){
lblRed[i].style.color = "hsl(0, 1%, 44%)";
}
for(let i = 0; i < textRed.length; i++){
textRed[i].style.borderColor = "hsl(0, 0%, 8%)";
}
}
function reseter(){
const resetDay = document.querySelector('#state-day');
const resetMonth = document.querySelector('#state-month');
const resetYear= document.querySelector('#state-years');
resetDay.textContent = '--';
resetMonth.textContent = '--';
resetYear.textContent = '--';
}
// function output date
function outputDate(years, months, days){
const dayOutput = document.querySelector('#state-day');
const monthOutput = document.querySelector('#state-month');
const yearOutput = document.querySelector('#state-years');
const date = new Date(years, months, days);
const currentDate = new Date();
const timeAge = currentDate.getTime() - date.getTime();
const ageMiliseconds = new Date(timeAge);
const age = Math.abs(ageMiliseconds.getUTCFullYear() - 1970);
const monthy = ageMiliseconds.getUTCMonth() + 1;
const nDays = currentDate.getDate() - date.getDate();
// Si el número de días es negativo, restamos un mes y lo sumamos al número de días
if (nDays < 0) {
monthy--;
nDays += new Date(currentDate.getFullYear(), date.getMonth(), 0).getDate();
}
printOutput(() => yearOutput, age);
printOutput(() => monthOutput, monthy);
printOutput(() => dayOutput, nDays);
}
//effect timer
function printOutput(paramPrint, valor){
let cronos = 0;
let printer = valor;
const temp = paramPrint();
for(let i=0;i<=printer;i++){
setTimeout(()=>{
cronos = i;
temp.textContent = `${cronos}`;
}, i * 100);
}
}