feat: enhance event forms with date formatting and registration link updates; add utility for local date formatting
This commit is contained in:
@@ -1,4 +1,106 @@
|
||||
/**
|
||||
* Formatea una fecha en formato legible en español, con opción a incluir hora, minutos y día de la semana.
|
||||
*
|
||||
* @param {string} fechaStr - Cadena de texto representando una fecha válida (ISO 8601 o similar).
|
||||
* @param {Object} [config] - Configuración opcional para mostrar hora, minutos y día de la semana.
|
||||
* @param {boolean} [config.horas=false] - Indica si se debe incluir la hora.
|
||||
* @param {boolean} [config.minutos=false] - Indica si se deben incluir los minutos (requiere config.horas).
|
||||
* @param {boolean} [config.diaSemana=false] - Indica si se debe incluir el día de la semana.
|
||||
* @returns {string} Fecha formateada en español.
|
||||
* @throws {Error} Lanza un error si la fecha proporcionada no es válida.
|
||||
*
|
||||
* @example
|
||||
* formatFecha('2025-03-25T14:30:00Z');
|
||||
* // Retorna: "25 de marzo del 2025"
|
||||
*
|
||||
* @example
|
||||
* formatFecha('2025-03-25T14:30:00Z', { horas: true });
|
||||
* // Retorna: "25 de marzo del 2025 - 14"
|
||||
*
|
||||
* @example
|
||||
* formatFecha('2025-03-25T14:30:00Z', { horas: true, minutos: true });
|
||||
* // Retorna: "25 de marzo del 2025 - 14:30"
|
||||
*
|
||||
* @example
|
||||
* formatFecha('2025-03-25T14:30:00Z', { diaSemana: true });
|
||||
* // Retorna: "martes, 25 de marzo del 2025"
|
||||
*/
|
||||
export function formatFecha(
|
||||
fechaStr: string,
|
||||
config: { horas?: boolean; minutos?: boolean; diaSemana?: boolean } = {
|
||||
|
||||
horas: false,
|
||||
minutos: false,
|
||||
diaSemana: false,
|
||||
}
|
||||
): string {
|
||||
const fecha = new Date(fechaStr);
|
||||
|
||||
if (isNaN(fecha.getTime())) {
|
||||
throw new Error(`Fecha inválida: ${fechaStr}`);
|
||||
}
|
||||
|
||||
const diasSemana = [
|
||||
'domingo',
|
||||
'lunes',
|
||||
'martes',
|
||||
'miércoles',
|
||||
'jueves',
|
||||
'viernes',
|
||||
'sábado',
|
||||
];
|
||||
const meses = [
|
||||
'enero',
|
||||
'febrero',
|
||||
'marzo',
|
||||
'abril',
|
||||
'mayo',
|
||||
'junio',
|
||||
'julio',
|
||||
'agosto',
|
||||
'septiembre',
|
||||
'octubre',
|
||||
'noviembre',
|
||||
'diciembre',
|
||||
];
|
||||
|
||||
const dia = fecha.getUTCDate();
|
||||
const mes = meses[fecha.getUTCMonth()];
|
||||
const año = fecha.getUTCFullYear();
|
||||
const hora = (fecha.getUTCHours() - 6 + 24) % 24;
|
||||
const minutos = fecha.getUTCMinutes().toString().padStart(2, '0');
|
||||
const diaSemana = diasSemana[fecha.getUTCDay()];
|
||||
|
||||
let fechaFormateada = '';
|
||||
|
||||
if (config.diaSemana) {
|
||||
fechaFormateada += `${diaSemana}, `;
|
||||
}
|
||||
|
||||
fechaFormateada += `${dia} de ${mes} del ${año}`;
|
||||
|
||||
if (config.horas) {
|
||||
fechaFormateada += ` - ${hora}`;
|
||||
if (config.minutos) {
|
||||
fechaFormateada += `:${minutos}`;
|
||||
}
|
||||
}
|
||||
|
||||
return fechaFormateada;
|
||||
}
|
||||
|
||||
export function formatearFecha(fecha: string, tipo: 'inicio' | 'fin'): string {
|
||||
const sufijo = tipo === 'inicio' ? 'T00:00:01' : 'T23:59:59';
|
||||
return `${fecha}${sufijo}`;
|
||||
}
|
||||
|
||||
export function formatDateLocal(date: Date): string {
|
||||
const pad = (n: number) => n.toString().padStart(2, '0');
|
||||
const yyyy = date.getFullYear();
|
||||
const MM = pad(date.getMonth() + 1);
|
||||
const dd = pad(date.getDate());
|
||||
const hh = pad(date.getHours());
|
||||
const mm = pad(date.getMinutes());
|
||||
|
||||
return `${yyyy}-${MM}-${dd}T${hh}:${mm}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user