41
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GoogleAppsScriptで日本の祝日を取得する

Last updated at Posted at 2014-10-05

ざっくりいうと、日本の祝日のカレンダーを取得してきて。今から10分後までにイベントがあるかを調べる感じです。

ちなみにholidaysはCalendarEvent型の配列、CalendarEventが持ってるメソッドとかは以下参照
https://developers.google.com/apps-script/reference/calendar/calendar-event?hl=ja

// 2つ目の引数のmonthは0始まりなので4は5月
function main() {
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 1, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 2, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 3, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 4, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 5, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 6, 12, 0, 0)));
}

/*
 * 今日が日本の祝日かどうか判定
 */
function isJapaneseHoliday(date) {
  // 今から10分後までで検索 (もっと短くてもいいかも)
  var startDate = new Date(date.getTime());
  var endDate = new Date(date.getTime() + (10*60*1000));

  var cal = CalendarApp.getCalendarById("ja.japanese#holiday@group.v.calendar.google.com");
  var holidays =  cal.getEvents(startDate, endDate);

  return holidays.length != 0;
}

mainを実行した結果ログ。
ちゃんと2020/05/01と2020/05/02だけfalseになっている。

[20-05-02 19:32:40:332 JST] false
[20-05-02 19:32:40:407 JST] false
[20-05-02 19:32:40:480 JST] true
[20-05-02 19:32:40:544 JST] true
[20-05-02 19:32:40:583 JST] true
[20-05-02 19:32:40:646 JST] true
41
35
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
41
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?