Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

138 rindas
3.2 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 OUTPUT_TIME_FORMAT = "HH:mm:ss";
  17. export const convertDateToString = (
  18. date: Date,
  19. format: string = OUTPUT_DATE_FORMAT,
  20. ) => {
  21. return dayjs(date).format(format);
  22. };
  23. export const convertDateArrayToString = (
  24. dateArray: number[],
  25. format: string = OUTPUT_DATE_FORMAT,
  26. needTime: boolean = false,
  27. ) => {
  28. if (dateArray.length === 6) {
  29. if (!needTime) {
  30. const dateString = `${dateArray[0]}-${dateArray[1]}-${dateArray[2]}`;
  31. return dayjs(dateString).format(format);
  32. }
  33. }
  34. if (dateArray.length === 3) {
  35. if (!needTime) {
  36. const dateString = `${dateArray[0]}-${dateArray[1]}-${dateArray[2]}`;
  37. return dayjs(dateString).format(format);
  38. }
  39. }
  40. };
  41. export const convertTimeArrayToString = (
  42. timeArray: number[],
  43. format: string = OUTPUT_TIME_FORMAT,
  44. needTime: boolean = false,
  45. ) => {
  46. let timeString = "";
  47. if (timeArray !== null && timeArray !== undefined) {
  48. const hour = timeArray[0] || 0;
  49. const minute = timeArray[1] || 0;
  50. timeString = dayjs()
  51. .set("hour", hour)
  52. .set("minute", minute)
  53. .set("second", 0)
  54. .format("HH:mm:ss");
  55. }
  56. return timeString;
  57. };
  58. const shortDateFormatter_en = new Intl.DateTimeFormat("en-HK", {
  59. weekday: "short",
  60. year: "numeric",
  61. month: "short",
  62. day: "numeric",
  63. });
  64. const shortDateFormatter_zh = new Intl.DateTimeFormat("zh-HK", {
  65. weekday: "long",
  66. year: "numeric",
  67. month: "numeric",
  68. day: "numeric",
  69. });
  70. export const shortDateFormatter = (locale?: string) => {
  71. switch (locale) {
  72. case "zh":
  73. return shortDateFormatter_zh;
  74. case "en":
  75. default:
  76. return shortDateFormatter_en;
  77. }
  78. };
  79. const clockFormatOptions: Intl.DateTimeFormatOptions = {
  80. year: "numeric",
  81. month: "long",
  82. day: "numeric",
  83. weekday: "long",
  84. hour: "2-digit",
  85. minute: "2-digit",
  86. second: "2-digit",
  87. hour12: true,
  88. };
  89. const clockTimeFormatter_en = new Intl.DateTimeFormat(
  90. "en-HK",
  91. clockFormatOptions,
  92. );
  93. const clockTimeformatter_zh = new Intl.DateTimeFormat(
  94. "zh-HK",
  95. clockFormatOptions,
  96. );
  97. export const clockTimeFormatter = (locale?: string) => {
  98. switch (locale) {
  99. case "zh":
  100. return clockTimeformatter_zh;
  101. case "en":
  102. default:
  103. return clockTimeFormatter_en;
  104. }
  105. };
  106. export function convertLocaleStringToNumber(numberString: string): number {
  107. const numberWithoutCommas = numberString.replace(/,/g, "");
  108. return parseFloat(numberWithoutCommas);
  109. }
  110. export function timestampToDateString(timestamp: string): string {
  111. const date = new Date(timestamp);
  112. const year = date.getFullYear();
  113. const month = String(date.getMonth() + 1).padStart(2, "0");
  114. const day = String(date.getDate()).padStart(2, "0");
  115. console.log(`${year}-${month}-${day}`);
  116. return `${year}-${month}-${day}`;
  117. }