|
- import Holidays from "date-holidays";
- import { HolidaysResult } from "../api/holidays";
- import dayjs from "dayjs";
- import arraySupport from "dayjs/plugin/arraySupport";
- import { INPUT_DATE_FORMAT } from "./formatUtil";
-
- dayjs.extend(arraySupport);
-
- const hd = new Holidays("HK");
-
- export const getPublicHolidaysForNYears = (years: number = 1, currYr?: number) => {
- return Array(years)
- .fill(undefined)
- .flatMap((_, index) => {
- const currentYear = currYr ?? new Date().getFullYear();
- const holidays = hd.getHolidays(currentYear + index);
- return holidays.map((ele) => {
- const tempDay = new Date(ele.date);
- const tempYear = tempDay.getFullYear();
- const tempMonth =
- tempDay.getMonth() + 1 < 10
- ? `0${tempDay.getMonth() + 1}`
- : tempDay.getMonth() + 1;
- const tempDate =
- tempDay.getDate() < 10 ? `0${tempDay.getDate()}` : tempDay.getDate();
- let tempName = "";
- switch (ele.name) {
- case "复活节":
- tempName = "復活節";
- break;
- case "劳动节":
- tempName = "勞動節";
- break;
- case "端午节":
- tempName = "端午節";
- break;
- case "重阳节":
- tempName = "重陽節";
- break;
- case "圣诞节后的第一个工作日":
- tempName = "聖誕節後的第一个工作日";
- break;
- default:
- tempName = ele.name;
- break;
- }
-
- return {
- date: `${tempYear}-${tempMonth}-${tempDate}`,
- title: tempName,
- extendedProps: { calendar: "holiday" },
- };
- });
- });
- };
-
- export const getHolidayForDate = (
- date: string,
- companyHolidays: HolidaysResult[] = [],
- ) => {
- const currentYearHolidays: { date: string; title: string }[] = companyHolidays
- .map((h) => ({
- title: h.name,
- // Dayjs use 0-index for months, but not our API
- date: dayjs([h.date[0], h.date[1] - 1, h.date[2]]).format(
- INPUT_DATE_FORMAT,
- ),
- }))
- .concat(getPublicHolidaysForNYears(1).concat());
-
- return currentYearHolidays.find((h) => h.date === date);
- };
|