From fe240df61c9c5634127d117bfec5c7583aa5c897 Mon Sep 17 00:00:00 2001 From: yarnom Date: Thu, 10 Jul 2025 11:56:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=B6=E9=97=B4=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/index.html | 71 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/static/index.html b/static/index.html index e3f099a..2035d75 100644 --- a/static/index.html +++ b/static/index.html @@ -544,22 +544,34 @@ try { // 按时间正序排列数据(从早到晚) data.sort((a, b) => { - const timeA = a.timestamp ? new Date(a.timestamp) : new Date(a.formatted_time); - const timeB = b.timestamp ? new Date(b.timestamp) : new Date(b.formatted_time); + const timeA = a.formatted_time ? new Date(a.formatted_time) : new Date(a.timestamp); + const timeB = b.formatted_time ? new Date(b.formatted_time) : new Date(b.timestamp); return timeA - timeB; }); const labels = data.map(item => { - // 解析时间字符串为本地时间 - const timeStr = item.formatted_time || item.timestamp; - const date = new Date(timeStr); + // 优先使用formatted_time,如果不存在则尝试使用timestamp + let timeStr = item.formatted_time || item.timestamp; - // 格式化为中文日期时间格式 - return date.getFullYear() + '/' + - (date.getMonth() + 1).toString().padStart(2, '0') + '/' + - date.getDate().toString().padStart(2, '0') + ' ' + - date.getHours().toString().padStart(2, '0') + ':' + - date.getMinutes().toString().padStart(2, '0'); + try { + // 解析时间字符串为本地时间 + const date = new Date(timeStr); + + // 检查日期是否有效 + if (isNaN(date.getTime())) { + return timeStr; // 如果无法解析,直接返回原始字符串 + } + + // 格式化为中文日期时间格式 + return date.getFullYear() + '/' + + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + + date.getDate().toString().padStart(2, '0') + ' ' + + date.getHours().toString().padStart(2, '0') + ':' + + date.getMinutes().toString().padStart(2, '0'); + } catch (e) { + console.error('图表时间解析错误:', e); + return timeStr; // 出错时返回原始字符串 + } }); // 是否使用自适应范围 @@ -654,20 +666,37 @@ tbody.innerHTML = ''; // 按时间倒序排列数据(从晚到早),这样最新的数据在表格顶部 - const sortedData = [...data].sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); + const sortedData = [...data].sort((a, b) => { + // 使用formatted_time进行排序,如果不存在则尝试使用timestamp + const timeA = a.formatted_time ? new Date(a.formatted_time) : new Date(a.timestamp); + const timeB = b.formatted_time ? new Date(b.formatted_time) : new Date(b.timestamp); + return timeB - timeA; + }); sortedData.forEach(item => { const row = document.createElement('tr'); - // 解析时间字符串为本地时间 - const date = new Date(item.timestamp); - const formattedDate = - date.getFullYear() + '/' + - (date.getMonth() + 1).toString().padStart(2, '0') + '/' + - date.getDate().toString().padStart(2, '0') + ' ' + - date.getHours().toString().padStart(2, '0') + ':' + - date.getMinutes().toString().padStart(2, '0') + ':' + - date.getSeconds().toString().padStart(2, '0'); + // 优先使用formatted_time,如果不存在则尝试使用timestamp + let formattedDate; + if (item.formatted_time) { + // 如果已经有格式化好的时间,直接使用 + formattedDate = item.formatted_time; + } else { + // 否则尝试解析timestamp并格式化 + try { + const date = new Date(item.timestamp); + formattedDate = + date.getFullYear() + '/' + + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + + date.getDate().toString().padStart(2, '0') + ' ' + + date.getHours().toString().padStart(2, '0') + ':' + + date.getMinutes().toString().padStart(2, '0') + ':' + + date.getSeconds().toString().padStart(2, '0'); + } catch (e) { + console.error('时间解析错误:', e); + formattedDate = '时间格式错误'; + } + } row.innerHTML = ` ${formattedDate}