From 7bf892db19e2c7bdd358d2c8914217df325c5a40 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Tue, 20 Jan 2026 11:06:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=97=A5=E6=9C=9F=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=87=BD=E5=BC=8F=EF=BC=8C=E7=A2=BA=E4=BF=9D?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=E5=AD=97=E4=B8=B2=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=81=BF=E5=85=8D=E6=99=82=E5=8D=80=E5=81=8F=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/js/utils/format.ts | 40 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/resources/js/utils/format.ts b/resources/js/utils/format.ts index 9c2084c..9e34f94 100644 --- a/resources/js/utils/format.ts +++ b/resources/js/utils/format.ts @@ -21,18 +21,15 @@ export const formatCurrency = (num: number): string => { */ export const formatDate = (date: string): string => { if (!date) return "-"; + // Assume date format is YYYY-MM-DD or YYYY-MM-DD HH:mm:ss const datePart = date.split("T")[0].split(" ")[0]; - const parts = datePart.split("-").map(Number); - if (parts.length < 3 || parts.some(isNaN)) return date; - - const [y, m, d] = parts; - // Initialize at noon to avoid timezone shifting issues - const dt = new Date(y, m - 1, d, 12, 0, 0); - const year = dt.getFullYear(); - const month = String(dt.getMonth() + 1).padStart(2, "0"); - const day = String(dt.getDate()).padStart(2, "0"); - - return `${year}/${month}/${day}`; + // Directly return the parsed string components to guarantee no timezone shift + const parts = datePart.split("-"); + if (parts.length === 3) { + return `${parts[0]}/${parts[1]}/${parts[2]}`; + } + // Fallback for unexpected formats + return datePart.replace(/-/g, "/"); }; /** @@ -41,18 +38,19 @@ export const formatDate = (date: string): string => { export const formatDateWithDayOfWeek = (date: string): string => { if (!date) return "-"; const datePart = date.split("T")[0].split(" ")[0]; - const parts = datePart.split("-").map(Number); - if (parts.length < 3 || parts.some(isNaN)) return date; + const parts = datePart.split("-"); - const [y, m, d] = parts; - // Initialize at noon to avoid timezone shifting issues - const dt = new Date(y, m - 1, d, 12, 0, 0); - const year = dt.getFullYear(); - const month = String(dt.getMonth() + 1).padStart(2, "0"); - const day = String(dt.getDate()).padStart(2, "0"); - const weekDay = dt.toLocaleDateString("zh-TW", { weekday: "short" }); + if (parts.length === 3) { + const [y, m, d] = parts.map(Number); + // Use noon to safely calculate the day of week + const dt = new Date(y, m - 1, d, 12, 0, 0); + const weekDay = dt.toLocaleDateString("zh-TW", { weekday: "short" }); - return `${year}/${month}/${day} (${weekDay})`; + // Return original string parts + calculated weekday + return `${parts[0]}/${parts[1]}/${parts[2]} (${weekDay})`; + } + + return datePart.replace(/-/g, "/"); }; /**