fix: 修复 timestamp2string1 跨年显示问题,仅在数据跨年时显示年份

This commit is contained in:
CaIon
2026-01-01 15:42:15 +08:00
parent 87a75b0565
commit a3c1bc6350
2 changed files with 32 additions and 12 deletions

View File

@@ -217,15 +217,12 @@ export function timestamp2string(timestamp) {
);
}
export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour', showYear = false) {
let date = new Date(timestamp * 1000);
// let year = date.getFullYear().toString();
let year = date.getFullYear();
let month = (date.getMonth() + 1).toString();
let day = date.getDate().toString();
let hour = date.getHours().toString();
if (day === '24') {
console.log('timestamp', timestamp);
}
if (month.length === 1) {
month = '0' + month;
}
@@ -235,11 +232,13 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
if (hour.length === 1) {
hour = '0' + hour;
}
let str = month + '-' + day;
// 仅在跨年时显示年份
let str = showYear ? year + '-' + month + '-' + day : month + '-' + day;
if (dataExportDefaultTime === 'hour') {
str += ' ' + hour + ':00';
} else if (dataExportDefaultTime === 'week') {
let nextWeek = new Date(timestamp * 1000 + 6 * 24 * 60 * 60 * 1000);
let nextWeekYear = nextWeek.getFullYear();
let nextMonth = (nextWeek.getMonth() + 1).toString();
let nextDay = nextWeek.getDate().toString();
if (nextMonth.length === 1) {
@@ -248,11 +247,20 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
if (nextDay.length === 1) {
nextDay = '0' + nextDay;
}
str += ' - ' + nextMonth + '-' + nextDay;
// 周视图结束日期也仅在跨年时显示年份
let nextStr = showYear ? nextWeekYear + '-' + nextMonth + '-' + nextDay : nextMonth + '-' + nextDay;
str += ' - ' + nextStr;
}
return str;
}
// 检查时间戳数组是否跨年
export function isDataCrossYear(timestamps) {
if (!timestamps || timestamps.length === 0) return false;
const years = new Set(timestamps.map(ts => new Date(ts * 1000).getFullYear()));
return years.size > 1;
}
export function downloadTextAsFile(text, filename) {
let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
let url = URL.createObjectURL(blob);