resolviendo errores

This commit is contained in:
Tatiana Villa Ema 2026-02-17 19:07:34 +01:00
parent ed8138782f
commit 981305d1df
1 changed files with 16 additions and 15 deletions

View File

@ -188,32 +188,33 @@ function renderTrend(data) {
// ==================== // ====================
function loadCurrentMonth() { function loadCurrentMonth() {
const ahora = new Date(); const ahora = new Date();
const currentYear = ahora.getFullYear(); const yearActual = ahora.getFullYear();
const currentMonth = ahora.getMonth(); const mesActual = ahora.getMonth();
const diaActual = ahora.getDate();
// 1. Calculamos el año objetivo (si selectedMonth es mayor al actual, asumimos año anterior o lógica de negocio) // El mes que queremos consultar
// Para simplificar tu oposición, usaremos el año actual. const yearBusqueda = yearActual;
const year = currentYear; const monthBusqueda = selectedMonth + 1;
const month = selectedMonth + 1;
// 2. Primer día del mes // 1. Primer día del mes (Siempre el 01)
const firstDay = `${year}-${String(month).padStart(2, "0")}-01`; const firstDay = `${yearBusqueda}-${String(monthBusqueda).padStart(2, "0")}-01`;
// 3. Cálculo del último día (evitando el desfase de toISOString) // 2. Calculamos el último día teórico del mes
let ultimoDiaObj = new Date(year, month, 0); let ultimoDiaObj = new Date(yearBusqueda, monthBusqueda, 0);
// LÓGICA DE SEGURIDAD: Si es el mes/año actual, el último día es HOY, no el fin de mes. // 3. VALIDACIÓN CRUCIAL: Si el mes seleccionado es el actual,
if (selectedMonth === currentMonth && year === currentYear) { // limitamos la búsqueda hasta HOY para evitar el Error 500 del servidor.
if (selectedMonth === mesActual && yearBusqueda === yearActual) {
ultimoDiaObj = ahora; ultimoDiaObj = ahora;
} }
// Formateo manual para evitar el error de -1 día por zona horaria (UTC) // 4. Formateo manual YYYY-MM-DD (Evita toISOString y sus desfases UTC)
const y = ultimoDiaObj.getFullYear(); const y = ultimoDiaObj.getFullYear();
const m = String(ultimoDiaObj.getMonth() + 1).padStart(2, "0"); const m = String(ultimoDiaObj.getMonth() + 1).padStart(2, "0");
const d = String(ultimoDiaObj.getDate()).padStart(2, "0"); const d = String(ultimoDiaObj.getDate()).padStart(2, "0");
const lastDay = `${y}-${m}-${d}`; const lastDay = `${y}-${m}-${d}`;
console.log("Cargando datos para:", monthNames[selectedMonth], `(${firstDay} a ${lastDay})`); console.log(`Petición: ${firstDay} hasta ${lastDay}`);
loadStats({ desde: firstDay, hasta: lastDay }); loadStats({ desde: firstDay, hasta: lastDay });
} }