Files
star-erp/resources/js/utils/format.ts
sky121113 239e547a5d
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 57s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
修正日期時區偏移導致顯示少一天的問題
2026-01-20 10:57:39 +08:00

139 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 格式化相關工具函式
*/
/**
* 格式化數字為千分位格式
*/
export const formatNumber = (num: number): string => {
return num.toLocaleString();
};
/**
* 格式化貨幣NT$
*/
export const formatCurrency = (num: number): string => {
return `NT$ ${num.toLocaleString()}`;
};
/**
* 格式化日期
*/
export const formatDate = (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 [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}`;
};
/**
* 格式化日期並包含星期
*/
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 [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})`;
};
/**
* 格式化發票號碼
* 例如AB12345678 -> AB-12345678
*/
export const formatInvoiceNumber = (invoice: string | null | undefined): string => {
if (!invoice) return "-";
const cleanInvoice = invoice.replace(/-/g, "");
if (/^[a-zA-Z]{2}\d+$/.test(cleanInvoice)) {
return `${cleanInvoice.slice(0, 2).toUpperCase()}-${cleanInvoice.slice(2)}`;
}
return invoice;
};
/**
* 獲取當前日期YYYY-MM-DD 格式)
*/
export const getCurrentDate = (): string => {
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}`;
};
/**
* 生成唯一 ID
*/
export const generateId = (): string => {
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
};
/**
* 生成撥補單號
*/
export const generateOrderNumber = (): string => {
return `TO${Date.now().toString().slice(-8)}`;
};
/**
* 生成批號
* 格式:{倉庫代碼}-{日期YYYYMMDD}-{流水號}
* 例如WH1-20251128-001
*/
export const generateBatchNumber = (
warehouseId: string,
date?: string,
sequence?: number
): string => {
const targetDate = date || getCurrentDate();
const dateStr = targetDate.replace(/-/g, "");
const seq = sequence || Math.floor(Math.random() * 1000);
const seqStr = seq.toString().padStart(3, "0");
return `WH${warehouseId}-${dateStr}-${seqStr}`;
};
/**
* 獲取當前日期時間YYYY-MM-DDTHH:mm 格式,用於 datetime-local input
*/
export const getCurrentDateTime = (): string => {
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");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day}T${hours}:${minutes}`;
};
/**
* 格式化日期時間顯示
*/
export const formatDateTime = (datetime: string): string => {
if (!datetime) return "-";
return new Date(datetime).toLocaleString("zh-TW", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
};