修正日期時區偏移導致顯示少一天的問題
This commit is contained in:
@@ -21,7 +21,17 @@ export const formatCurrency = (num: number): string => {
|
||||
*/
|
||||
export const formatDate = (date: string): string => {
|
||||
if (!date) return "-";
|
||||
return new Date(date).toLocaleDateString("zh-TW");
|
||||
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;
|
||||
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}`;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -29,12 +39,19 @@ export const formatDate = (date: string): string => {
|
||||
*/
|
||||
export const formatDateWithDayOfWeek = (date: string): string => {
|
||||
if (!date) return "-";
|
||||
return new Date(date).toLocaleDateString("zh-TW", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
weekday: "short",
|
||||
});
|
||||
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;
|
||||
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" });
|
||||
|
||||
return `${year}/${month}/${day} (${weekDay})`;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -54,7 +71,11 @@ export const formatInvoiceNumber = (invoice: string | null | undefined): string
|
||||
* 獲取當前日期(YYYY-MM-DD 格式)
|
||||
*/
|
||||
export const getCurrentDate = (): string => {
|
||||
return new Date().toISOString().split("T")[0];
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
return `${year}-${month}-${day}`;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user