Para mostrar las fechas en formato yyyy-MM-dd HH:mm:ss en tu tabla, necesitas modificar la función que procesa los datos de respuesta. Aquí te muestro cómo hacerlo:
- Primero, crea una función auxiliar para formatear las fechas:
function formatDate(dateString) {
// Si la fecha viene en formato /Date(timestamp)/
if (dateString.startsWith("/Date(")) {
const timestamp = parseInt(dateString.match(/\d+/)[0]);
const date = new Date(timestamp);
return date.toISOString().replace('T', ' ').substring(0, 19);
}
// Si la fecha ya viene en formato ISO
else if (dateString.includes('T')) {
return dateString.replace('T', ' ').substring(0, 19);
}
// Si viene en otro formato, intenta parsearlo
else {
const date = new Date(dateString);
return date.toISOString().replace('T', ' ').substring(0, 19);
}
}
- Luego, modifica la parte donde creas las filas de la tabla para usar esta función:
// En la función success de btnBuscarPesos
success: function (response) {
if (response.success) {
$('#resultadosPesos').html('');
let tabla = `
<table class="table table-striped table-dark" style="width:100%">
<thead>
<tr>
<th>BASCULA_SALA</th>
<th>PESO_SALA</th>
<th>FECHA_SALA</th>
<th>BASCULA_ABAJO</th>
<th>PESO_ABAJO</th>
<th>FECHA_ABAJO</th>
</tr>
</thead>
<tbody>
${response.data.map(item => `
<tr data-id="${item.ELEMENT_ID}">
<td>${item.BASCULA_SALA}</td>
<td>${item.PESO_SALA}</td>
<td>${formatDate(item.FECHA_SALA)}</td>
<td>${item.BASCULA_ABAJO}</td>
<td>${item.PESO_ABAJO}</td>
<td>${formatDate(item.FECHA_ABAJO)}</td>
</tr>
`).join("")}
</tbody>
</table>`;
$('#resultadosPesos').html(
`<h3 class="titulo-centrado">Pesos</h3>${tabla}`
);
} else {
alert("No se encontraron datos.");
alert(response.message);
}
}
-
Asegúrate de que tu backend esté enviando las fechas en un formato que pueda ser parseado. Si las fechas vienen en formato /Date(timestamp)/, la función formatDate que proporcioné ya maneja ese caso.
-
También puedes aplicar el mismo formato a la función formatDateTime que usas para establecer los valores iniciales de los inputs:
const formatDateTime = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};