|
- 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 OUTPUT_TIME_FORMAT = "HH:mm:ss";
-
- 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);
- }
- }
- if (dateArray.length === 3) {
- if (!needTime) {
- const dateString = `${dateArray[0]}-${dateArray[1]}-${dateArray[2]}`;
- return dayjs(dateString).format(format);
- }
- }
- };
-
- export const convertTimeArrayToString = (
- timeArray: number[],
- format: string = OUTPUT_TIME_FORMAT,
- needTime: boolean = false,
- ) => {
- let timeString = "";
-
- if (timeArray !== null && timeArray !== undefined) {
- const hour = timeArray[0] || 0;
- const minute = timeArray[1] || 0;
-
- timeString = dayjs()
- .set("hour", hour)
- .set("minute", minute)
- .set("second", 0)
- .format("HH:mm:ss");
- }
-
- return timeString;
- };
-
- 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;
- }
- };
-
- const clockFormatOptions: Intl.DateTimeFormatOptions = {
- year: "numeric",
- month: "long",
- day: "numeric",
- weekday: "long",
- hour: "2-digit",
- minute: "2-digit",
- second: "2-digit",
- hour12: true,
- };
-
- const clockTimeFormatter_en = new Intl.DateTimeFormat(
- "en-HK",
- clockFormatOptions,
- );
- const clockTimeformatter_zh = new Intl.DateTimeFormat(
- "zh-HK",
- clockFormatOptions,
- );
-
- export const clockTimeFormatter = (locale?: string) => {
- switch (locale) {
- case "zh":
- return clockTimeformatter_zh;
- case "en":
- default:
- return clockTimeFormatter_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}`;
- }
|