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.
 
 

73 rindas
2.2 KiB

  1. import Holidays from "date-holidays";
  2. import { HolidaysResult } from "../api/holidays";
  3. import dayjs from "dayjs";
  4. import arraySupport from "dayjs/plugin/arraySupport";
  5. import { INPUT_DATE_FORMAT } from "./formatUtil";
  6. dayjs.extend(arraySupport);
  7. const hd = new Holidays("HK");
  8. export const getPublicHolidaysForNYears = (years: number = 1, currYr?: number) => {
  9. return Array(years)
  10. .fill(undefined)
  11. .flatMap((_, index) => {
  12. const currentYear = currYr ?? new Date().getFullYear();
  13. const holidays = hd.getHolidays(currentYear + index);
  14. return holidays.map((ele) => {
  15. const tempDay = new Date(ele.date);
  16. const tempYear = tempDay.getFullYear();
  17. const tempMonth =
  18. tempDay.getMonth() + 1 < 10
  19. ? `0${tempDay.getMonth() + 1}`
  20. : tempDay.getMonth() + 1;
  21. const tempDate =
  22. tempDay.getDate() < 10 ? `0${tempDay.getDate()}` : tempDay.getDate();
  23. let tempName = "";
  24. switch (ele.name) {
  25. case "复活节":
  26. tempName = "復活節";
  27. break;
  28. case "劳动节":
  29. tempName = "勞動節";
  30. break;
  31. case "端午节":
  32. tempName = "端午節";
  33. break;
  34. case "重阳节":
  35. tempName = "重陽節";
  36. break;
  37. case "圣诞节后的第一个工作日":
  38. tempName = "聖誕節後的第一个工作日";
  39. break;
  40. default:
  41. tempName = ele.name;
  42. break;
  43. }
  44. return {
  45. date: `${tempYear}-${tempMonth}-${tempDate}`,
  46. title: tempName,
  47. extendedProps: { calendar: "holiday" },
  48. };
  49. });
  50. });
  51. };
  52. export const getHolidayForDate = (
  53. date: string,
  54. companyHolidays: HolidaysResult[] = [],
  55. ) => {
  56. const currentYearHolidays: { date: string; title: string }[] = companyHolidays
  57. .map((h) => ({
  58. title: h.name,
  59. // Dayjs use 0-index for months, but not our API
  60. date: dayjs([h.date[0], h.date[1] - 1, h.date[2]]).format(
  61. INPUT_DATE_FORMAT,
  62. ),
  63. }))
  64. .concat(getPublicHolidaysForNYears(1).concat());
  65. return currentYearHolidays.find((h) => h.date === date);
  66. };