Skip to content

Commit 2c99a4b

Browse files
committed
#14 typescript
1 parent 87fae4e commit 2c99a4b

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Definimos algunas interfaces útiles
2+
interface DateFormats {
3+
dayMonthYear: string;
4+
hourMinuteSecond: string;
5+
dayOfYear: number;
6+
dayOfWeek: string;
7+
monthName: string;
8+
longFormat: string;
9+
isoFormat: string;
10+
twelveHourFormat: string;
11+
unixTimestamp: number;
12+
quarterOfYear: number;
13+
}
14+
15+
// Definimos algunos tipos de utilidad
16+
type MonthName = 'Enero' | 'Febrero' | 'Marzo' | 'Abril' | 'Mayo' | 'Junio' |
17+
'Julio' | 'Agosto' | 'Septiembre' | 'Octubre' | 'Noviembre' | 'Diciembre';
18+
type DayOfWeek = 'Domingo' | 'Lunes' | 'Martes' | 'Miércoles' | 'Jueves' | 'Viernes' | 'Sábado';
19+
20+
// Creamos algunas constantes con tipos
21+
const DAYS_OF_WEEK: DayOfWeek[] = [
22+
'Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'
23+
];
24+
25+
const MONTHS: MonthName[] = [
26+
'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
27+
'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'
28+
];
29+
30+
class DateCalculator {
31+
private currentDate: Date;
32+
private birthDate: Date;
33+
34+
constructor(birthDate: Date) {
35+
this.currentDate = new Date();
36+
this.birthDate = birthDate;
37+
}
38+
39+
calculateYearsBetween(): number {
40+
let years = this.currentDate.getFullYear() - this.birthDate.getFullYear();
41+
const currentMonth = this.currentDate.getMonth();
42+
const birthMonth = this.birthDate.getMonth();
43+
44+
// Ajustamos si aún no hemos llegado al mes y día de nacimiento este año
45+
if (currentMonth < birthMonth ||
46+
(currentMonth === birthMonth &&
47+
this.currentDate.getDate() < this.birthDate.getDate())) {
48+
years--;
49+
}
50+
51+
return years;
52+
}
53+
54+
formatDates(): DateFormats {
55+
return {
56+
dayMonthYear: this.formatDate(this.birthDate, 'dd/MM/yyyy'),
57+
hourMinuteSecond: this.formatTime(this.birthDate),
58+
dayOfYear: this.getDayOfYear(this.birthDate),
59+
dayOfWeek: DAYS_OF_WEEK[this.birthDate.getDay()],
60+
monthName: MONTHS[this.birthDate.getMonth()],
61+
longFormat: this.getLongFormat(this.birthDate),
62+
isoFormat: this.birthDate.toISOString(),
63+
twelveHourFormat: this.get12HourFormat(this.birthDate),
64+
unixTimestamp: Math.floor(this.birthDate.getTime() / 1000),
65+
quarterOfYear: Math.floor(this.birthDate.getMonth() / 3) + 1
66+
};
67+
}
68+
69+
private formatDate(date: Date, format: string): string {
70+
const day = String(date.getDate()).padStart(2, '0');
71+
const month = String(date.getMonth() + 1).padStart(2, '0');
72+
const year = date.getFullYear();
73+
74+
return format
75+
.replace('dd', day)
76+
.replace('MM', month)
77+
.replace('yyyy', String(year));
78+
}
79+
80+
private formatTime(date: Date): string {
81+
return date.toLocaleTimeString('es-ES', {
82+
hour12: false,
83+
hour: '2-digit',
84+
minute: '2-digit',
85+
second: '2-digit'
86+
});
87+
}
88+
89+
private getDayOfYear(date: Date): number {
90+
const startOfYear = new Date(date.getFullYear(), 0, 0);
91+
const diff = date.getTime() - startOfYear.getTime();
92+
const oneDay = 1000 * 60 * 60 * 24;
93+
return Math.floor(diff / oneDay);
94+
}
95+
96+
private getLongFormat(date: Date): string {
97+
const dayOfWeek = DAYS_OF_WEEK[date.getDay()];
98+
const dayOfMonth = date.getDate();
99+
const month = MONTHS[date.getMonth()];
100+
const year = date.getFullYear();
101+
102+
return `${dayOfWeek}, ${dayOfMonth} de ${month} de ${year}`;
103+
}
104+
105+
private get12HourFormat(date: Date): string {
106+
return date.toLocaleTimeString('es-ES', { hour12: true });
107+
}
108+
}
109+
110+
// Uso del código
111+
const birthDate = new Date(1990, 4, 15, 14, 30, 0); // Mes 4 es Mayo (0-11)
112+
const calculator = new DateCalculator(birthDate);
113+
114+
// PARTE 1: Calcular años transcurridos
115+
const yearsPassedTesting = (): void => {
116+
console.log('Fecha actual:', new Date().toLocaleString());
117+
console.log('Fecha de nacimiento:', birthDate.toLocaleString());
118+
console.log('Años transcurridos:', calculator.calculateYearsBetween());
119+
};
120+
121+
// PARTE 2: Mostrar diferentes formatos de fecha
122+
const showFormattedDates = (): void => {
123+
const formats = calculator.formatDates();
124+
125+
console.log('\nDIFERENTES FORMATOS DE FECHA:');
126+
console.log('1. Día, mes y año:', formats.dayMonthYear);
127+
console.log('2. Hora, minuto y segundo:', formats.hourMinuteSecond);
128+
console.log('3. Día del año:', formats.dayOfYear);
129+
console.log('4. Día de la semana:', formats.dayOfWeek);
130+
console.log('5. Nombre del mes:', formats.monthName);
131+
console.log('6. Formato largo:', formats.longFormat);
132+
console.log('7. Formato ISO:', formats.isoFormat);
133+
console.log('8. Formato 12 horas:', formats.twelveHourFormat);
134+
console.log('9. Unix Timestamp:', formats.unixTimestamp);
135+
console.log('10. Trimestre del año:', formats.quarterOfYear);
136+
};
137+
138+
// Ejecutamos las funciones
139+
yearsPassedTesting();
140+
showFormattedDates();

0 commit comments

Comments
 (0)