import dayjs from "dayjs"; export const manhourFormatter = new Intl.NumberFormat("en-HK", { minimumFractionDigits: 2, maximumFractionDigits: 2, }); export const moneyFormatter = new Intl.NumberFormat("en-HK", { style: "currency", currency: "HKD", }); export const percentFormatter = new Intl.NumberFormat("en-HK", { style: "percent", maximumFractionDigits: 2, }); export const INPUT_DATE_FORMAT = "YYYY-MM-DD"; export const OUTPUT_DATE_FORMAT = "YYYY/MM/DD"; export const convertDateToString = (date: Date, format: string = OUTPUT_DATE_FORMAT) => { return dayjs(date).format(format) } export const convertDateArrayToString = (dateArray: number[], format: string = OUTPUT_DATE_FORMAT, needTime: boolean = false) => { if (dateArray.length === 6) { if (!needTime) { const dateString = `${dateArray[0]}-${dateArray[1]}-${dateArray[2]}` return dayjs(dateString).format(format) } } } const shortDateFormatter_en = new Intl.DateTimeFormat("en-HK", { weekday: "short", year: "numeric", month: "short", day: "numeric", }); const shortDateFormatter_zh = new Intl.DateTimeFormat("zh-HK", { weekday: "long", year: "numeric", month: "numeric", day: "numeric", }); export const shortDateFormatter = (locale?: string) => { switch (locale) { case "zh": return shortDateFormatter_zh; case "en": default: return shortDateFormatter_en; } }; export function convertLocaleStringToNumber(numberString: string): number { const numberWithoutCommas = numberString.replace(/,/g, ""); return parseFloat(numberWithoutCommas); } export function timestampToDateString(timestamp: string): string { const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); console.log(`${year}-${month}-${day}`) return `${year}-${month}-${day}`; }