サンプルコード
以下のコードを参照。
コメントにも書いたが、Google Calendarでは、全日の予定の日付は内部的にUTCになっているらしい。JSTだと検索条件がずれて一日違いになっている。
Google Calendarの「日本の祝日」は、機械的処理を目的としたものではないため、将来的にこの方法で祝日判定できることは保証されないので注意(たとえば、「この日は祝日ではなくなりました」というイベントが追加されるかもしれない)。あくまで勉強用に。
const { google } = require('googleapis');
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar.readonly'],
});
const calendar = google.calendar({version: 'v3', auth: auth});
const isHoliday = async (dateString) => {
// Google Calendarの日付は内部的にUTCになっている。JSTだと検索条件がずれるので注意
const date = dayjs.utc(dateString);
const response = await calendar.events.list({
calendarId: "japanese__ja@holiday.calendar.google.com",
timeMin: date.toISOString(),
timeMax: date.clone().add(1, "hour").toISOString(),
});
return response.data.items.length > 0;
};