diff --git a/ops-platform/src/utils/date.ts b/ops-platform/src/utils/date.ts index f0f02ed..2fda186 100644 --- a/ops-platform/src/utils/date.ts +++ b/ops-platform/src/utils/date.ts @@ -1,7 +1,14 @@ const TZ = 'Asia/Shanghai' +function toUTC(value: string): string { + return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value) && !/[Z+\-]\d*$/.test(value) + ? value + 'Z' + : value +} + export function formatTime(value: string | number | null | undefined): string { if (value === null || value === undefined || value === '') return '-' - const date = new Date(value as string | number) + const input = typeof value === 'string' ? toUTC(value) : value + const date = new Date(input) return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ }) } diff --git a/tenant-platform/src/utils/date.ts b/tenant-platform/src/utils/date.ts index f0f02ed..9d98f5c 100644 --- a/tenant-platform/src/utils/date.ts +++ b/tenant-platform/src/utils/date.ts @@ -1,7 +1,17 @@ const TZ = 'Asia/Shanghai' +// Backend serializes LocalDateTime without timezone (Jackson time-zone: UTC). +// Bare ISO strings like "2026-05-21T14:30:00" are treated as local time by browsers, +// so we append 'Z' to force UTC parsing before converting to Beijing time. +function toUTC(value: string): string { + return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value) && !/[Z+\-]\d*$/.test(value) + ? value + 'Z' + : value +} + export function formatTime(value: string | number | null | undefined): string { if (value === null || value === undefined || value === '') return '-' - const date = new Date(value as string | number) + const input = typeof value === 'string' ? toUTC(value) : value + const date = new Date(input) return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ }) }