Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

71 рядки
1.9 KiB

  1. import dayjs from "dayjs";
  2. export const manhourFormatter = new Intl.NumberFormat("en-HK", {
  3. minimumFractionDigits: 2,
  4. maximumFractionDigits: 2,
  5. });
  6. export const moneyFormatter = new Intl.NumberFormat("en-HK", {
  7. style: "currency",
  8. currency: "HKD",
  9. });
  10. export const percentFormatter = new Intl.NumberFormat("en-HK", {
  11. style: "percent",
  12. maximumFractionDigits: 2,
  13. });
  14. export const INPUT_DATE_FORMAT = "YYYY-MM-DD";
  15. export const OUTPUT_DATE_FORMAT = "YYYY/MM/DD";
  16. export const convertDateToString = (date: Date, format: string = OUTPUT_DATE_FORMAT) => {
  17. return dayjs(date).format(format)
  18. }
  19. export const convertDateArrayToString = (dateArray: number[], format: string = OUTPUT_DATE_FORMAT, needTime: boolean = false) => {
  20. if (dateArray.length === 6) {
  21. if (!needTime) {
  22. const dateString = `${dateArray[0]}-${dateArray[1]}-${dateArray[2]}`
  23. return dayjs(dateString).format(format)
  24. }
  25. }
  26. }
  27. const shortDateFormatter_en = new Intl.DateTimeFormat("en-HK", {
  28. weekday: "short",
  29. year: "numeric",
  30. month: "short",
  31. day: "numeric",
  32. });
  33. const shortDateFormatter_zh = new Intl.DateTimeFormat("zh-HK", {
  34. weekday: "long",
  35. year: "numeric",
  36. month: "numeric",
  37. day: "numeric",
  38. });
  39. export const shortDateFormatter = (locale?: string) => {
  40. switch (locale) {
  41. case "zh":
  42. return shortDateFormatter_zh;
  43. case "en":
  44. default:
  45. return shortDateFormatter_en;
  46. }
  47. };
  48. export function convertLocaleStringToNumber(numberString: string): number {
  49. const numberWithoutCommas = numberString.replace(/,/g, "");
  50. return parseFloat(numberWithoutCommas);
  51. }
  52. export function timestampToDateString(timestamp: string): string {
  53. const date = new Date(timestamp);
  54. const year = date.getFullYear();
  55. const month = String(date.getMonth() + 1).padStart(2, "0");
  56. const day = String(date.getDate()).padStart(2, "0");
  57. console.log(`${year}-${month}-${day}`)
  58. return `${year}-${month}-${day}`;
  59. }