Skip to content

Commit b5e74c2

Browse files
committed
14 - js - FECHAS
1 parent 052865e commit b5e74c2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
_____________________________________
3+
https://github.com/kenysdev
4+
2024 - JavaScript
5+
_______________________________________
6+
#14 FECHAS
7+
---------------------------------------
8+
* EJERCICIO:
9+
 * Crea dos variables utilizando los objetos fecha (date, o semejante) de tu lenguaje:
10+
 * - Una primera que represente la fecha (día, mes, año, hora, minuto, segundo) actual.
11+
 * - Una segunda que represente tu fecha de nacimiento (te puedes inventar la hora).
12+
 * Calcula cuántos años han transcurrido entre ambas fechas.
13+
 *
14+
 * DIFICULTAD EXTRA (opcional):
15+
 * Utilizando la fecha de tu cumpleaños, formatéala y muestra su resultado de
16+
 * 10 maneras diferentes. Por ejemplo:
17+
 * - Día, mes y año.
18+
 * - Hora, minuto y segundo.
19+
 * - Día de año.
20+
 * - Día de la semana.
21+
 * - Nombre del mes.
22+
 * (lo que se te ocurra...)
23+
*/
24+
// ________________________________________________________
25+
const { DateTime } = require("luxon");
26+
27+
const birthDate = DateTime.fromISO("1995-10-20T02:30:00");
28+
const currentDate = DateTime.now();
29+
30+
// Diferencia precisa
31+
const diff = currentDate.diff(birthDate, ["years", "months", "days"]);
32+
33+
console.log(`
34+
Juanito tiene:
35+
${Math.floor(diff.years)} años,
36+
${Math.floor(diff.months)} meses y
37+
${Math.floor(diff.days)} días.
38+
`);
39+
40+
// ________________________________________________________
41+
// DIFICULTAD EXTRA
42+
43+
console.log(`
44+
1. Predeterminado -> ${birthDate.toString()}
45+
2. dd/mm/yyyy -> ${birthDate.toFormat("dd/MM/yyyy")}
46+
3. dd-mm-yyyy -> ${birthDate.toFormat("dd-MM-yyyy")}
47+
4. Nombre del mes -> ${birthDate.toFormat("MMMM")}
48+
5. Mes abreviado -> ${birthDate.toFormat("MMM")}
49+
6. Nombre dia -> ${birthDate.toFormat("cccc")}
50+
7. Dia abreviado -> ${birthDate.toFormat("ccc")}
51+
8. Hora(12 horas) -> ${birthDate.toFormat("hh:mm:ss a")}
52+
9. Hora(24 horas) -> ${birthDate.toFormat("HH:mm:ss")}
53+
0. personalizado -> Born on ${birthDate.toFormat("cccc, dd 'of' MMMM yyyy at hh:mm:ss a")}
54+
`);

0 commit comments

Comments
 (0)