修正日期格式化函式,確保直接使用字串解析避免時區偏移
This commit is contained in:
@@ -21,18 +21,15 @@ export const formatCurrency = (num: number): string => {
|
|||||||
*/
|
*/
|
||||||
export const formatDate = (date: string): string => {
|
export const formatDate = (date: string): string => {
|
||||||
if (!date) return "-";
|
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 datePart = date.split("T")[0].split(" ")[0];
|
||||||
const parts = datePart.split("-").map(Number);
|
// Directly return the parsed string components to guarantee no timezone shift
|
||||||
if (parts.length < 3 || parts.some(isNaN)) return date;
|
const parts = datePart.split("-");
|
||||||
|
if (parts.length === 3) {
|
||||||
const [y, m, d] = parts;
|
return `${parts[0]}/${parts[1]}/${parts[2]}`;
|
||||||
// Initialize at noon to avoid timezone shifting issues
|
}
|
||||||
const dt = new Date(y, m - 1, d, 12, 0, 0);
|
// Fallback for unexpected formats
|
||||||
const year = dt.getFullYear();
|
return datePart.replace(/-/g, "/");
|
||||||
const month = String(dt.getMonth() + 1).padStart(2, "0");
|
|
||||||
const day = String(dt.getDate()).padStart(2, "0");
|
|
||||||
|
|
||||||
return `${year}/${month}/${day}`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,18 +38,19 @@ export const formatDate = (date: string): string => {
|
|||||||
export const formatDateWithDayOfWeek = (date: string): string => {
|
export const formatDateWithDayOfWeek = (date: string): string => {
|
||||||
if (!date) return "-";
|
if (!date) return "-";
|
||||||
const datePart = date.split("T")[0].split(" ")[0];
|
const datePart = date.split("T")[0].split(" ")[0];
|
||||||
const parts = datePart.split("-").map(Number);
|
const parts = datePart.split("-");
|
||||||
if (parts.length < 3 || parts.some(isNaN)) return date;
|
|
||||||
|
|
||||||
const [y, m, d] = parts;
|
if (parts.length === 3) {
|
||||||
// Initialize at noon to avoid timezone shifting issues
|
const [y, m, d] = parts.map(Number);
|
||||||
const dt = new Date(y, m - 1, d, 12, 0, 0);
|
// Use noon to safely calculate the day of week
|
||||||
const year = dt.getFullYear();
|
const dt = new Date(y, m - 1, d, 12, 0, 0);
|
||||||
const month = String(dt.getMonth() + 1).padStart(2, "0");
|
const weekDay = dt.toLocaleDateString("zh-TW", { weekday: "short" });
|
||||||
const day = String(dt.getDate()).padStart(2, "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, "/");
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user