-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfecha_hora.html
152 lines (118 loc) · 4.64 KB
/
fecha_hora.html
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
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calendario</title>
<style>
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 3px;
text-align: center;
}
th {
font-weight: bold;
background-color: #E6E6E6;
}
</style>
</head>
<body>
<div id="calendario"></div>
<div id="reloj"></div>
<script>
let date = new Date();
document.body.innerHTML += `<div>Fecha 1 (Sin parametros): ${date}</div>`;
let date1 = new Date(1000 * 60 * 24); // 1 seg = 1000 milisegundos
document.body.innerHTML += `<div>Fecha 2 (Milisegundos): ${date1}</div>`;
let date2 = new Date('2021-05-24');
document.body.innerHTML += `<div>Fecha 2 (Fecha String): ${date2}</div>`;
// Las fechas se autocorrigen
let date3 = new Date(2022, 6, 5);
document.body.innerHTML += `<div>Fecha 2 (Fecha Parametros): ${date3}</div>`;
date3.setDate(date3.getDate() + 3);
//Metodos de Fechas
console.log('Año: '+date.getFullYear());
console.log('Mes: '+date.getMonth());
console.log('Dia: '+date.getDate());
console.log('Horas: '+date.getHours());
console.log('Minutos: '+date.getMinutes());
console.log('Segundos: '+date.getSeconds());
console.log('N° Dia (0 - 6): '+date.getDay());
console.log('Tinestamp: '+date.getTime());
console.log('Fecha Sumada (+3 dias): '+date3);
console.log('Fecha Convertida Timestamp: '+(+date));
// Rendimiento en la ejecucio de Fechas
let start = new Date();
for (let i = 0; i < 100000; i++) {
let doSomething = i * i * i;
}
let end = new Date();
console.log(`Tiempo transcurrido es de ${end - start} ms`);
// Resta de Fechas
let primeraFecha = new Date(2022, 08, 25);
let segundaFecha = new Date(2022, 08, 23);
let resta = new Date(primeraFecha - segundaFecha);
console.log('Fecha restada: '+resta.getDate());
/*
* Tareas Realizadas
*/
// Tarea 1
console.warn('Generar una Fecha');
let tarea1 = new Date(2012, 01, 20, 3, 12);
console.log('Tarea 1: '+tarea1);
// Tarea 2
console.warn('Dias en Español');
let dias = ['DOMINGO', 'LUNES', 'MARTES', 'MIERCOLES', 'JUEVES', 'VIERNES', 'SABADO'];
function getWeekDay(date) {
return dias[date.getDay()];
}
let tarea2 = new Date(2022, 7, 31);
console.log('Tarea 2: '+getWeekDay(tarea2));
// Tarea 4
console.warn('Restar dias a una fecha');
function getDateAgo(date, restarDias) {
let newDate = new Date(date);
newDate.setDate(date.getDate() - restarDias);
return newDate;
}
let tarea4 = new Date(2015, 0, 2);
console.log('Tarea 4: '+getDateAgo(tarea4, 3));
// Tarea 5
console.warn('Ultimo dia de un mes');
function getLastDayOfMonth(year, month) {
//Recuerda correccion de fechas
// let fecha = new Date(year, month - 1, 0);
let fecha = new Date(year, month );
fecha.setMonth(fecha.getMonth() + 1);
fecha.setDate(fecha.getDate() - 1);
return fecha.getDate();
}
console.log('Tarea 5: '+getLastDayOfMonth(2012, 1));
// Dias de un mes
console.warn('Dias de un mes');
let calendario = new Date(2022, 3);
function genereteCalendar(date) {
// Estamos usando autocorreccion
// let nuevaFecha = new Date(date.getFullYear(), date.getMonth() + 1, 0);
// console.log(nuevaFecha);
// for (let i = 1; i <= nuevaFecha.getDate(); i++ ) {
// console.log('Dia: '+i);
// }
// while (date.getMonth()) {
// }
}
genereteCalendar(calendario);
// Creando un reloj
let reloj = document.querySelector('#reloj');
setInterval(() => {
let fechaReloj = new Date();
reloj.textContent = fechaReloj.getHours()+':'+fechaReloj.getMinutes()+':'+String(fechaReloj.getSeconds()).padStart(2, '0');
}, 1000);
</script>
</body>
</html>