Skip to content

Commit 7c69655

Browse files
committed
✨ Add typescript solution challenge-14
1 parent 8285c5b commit 7c69655

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

2024/14-fechas/solution.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Crear una variable con la fecha actual
2+
const fechaActual: Date = new Date();
3+
4+
// Crear una variable con tu fecha de nacimiento (puedes modificar esta fecha)
5+
const fechaNacimiento: Date = new Date('2001-08-24T19:30:00');
6+
7+
// Calcular la diferencia en milisegundos
8+
const diferenciaTiempo: number = fechaActual.getTime() - fechaNacimiento.getTime();
9+
10+
// Convertir la diferencia en milisegundos a años
11+
const milisegundosPorAño: number = 1000 * 60 * 60 * 24 * 365.25; // Considerando años bisiestos
12+
const añosTranscurridos: number = Math.floor(diferenciaTiempo / milisegundosPorAño);
13+
14+
console.log(`Han transcurrido ${añosTranscurridos} años desde tu nacimiento.`);
15+
16+
// Formatear y mostrar la fecha de nacimiento de 10 maneras diferentes
17+
console.log(
18+
`1. Día, mes y año: ${fechaNacimiento.getDate()}/${
19+
fechaNacimiento.getMonth() + 1
20+
}/${fechaNacimiento.getFullYear()}`
21+
);
22+
console.log(
23+
`2. Hora, minuto y segundo: ${fechaNacimiento.getHours()}:${fechaNacimiento.getMinutes()}:${fechaNacimiento.getSeconds()}`
24+
);
25+
console.log(
26+
`3. Día del año: ${Math.ceil(
27+
(fechaNacimiento.getTime() - new Date(fechaNacimiento.getFullYear(), 0, 1).getTime())
28+
/ (1000 * 60 * 60 * 24)
29+
)}`
30+
);
31+
console.log(
32+
`4. Día de la semana: ${fechaNacimiento.toLocaleString('es-ES', {
33+
weekday: 'long',
34+
})}`
35+
);
36+
console.log(
37+
`5. Nombre del mes: ${fechaNacimiento.toLocaleString('es-ES', {
38+
month: 'long',
39+
})}`
40+
);
41+
console.log(`6. Año completo: ${fechaNacimiento.getFullYear()}`);
42+
console.log(`7. Mes (número): ${fechaNacimiento.getMonth() + 1}`);
43+
console.log(`8. Día (número): ${fechaNacimiento.getDate()}`);
44+
console.log(`9. Fecha ISO: ${fechaNacimiento.toISOString()}`);
45+
console.log(`10. Fecha local: ${fechaNacimiento.toLocaleDateString('es-ES')}`);

0 commit comments

Comments
 (0)