LoginSignup
2
1

More than 3 years have passed since last update.

Google Calendar APIで祝日判定をする

Last updated at Posted at 2020-10-29

サンプルコード

以下のコードを参照。

コメントにも書いたが、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;
};
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1