6
6

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 5 years have passed since last update.

Node.jsにてカレンダーのicsファイルの作成

Last updated at Posted at 2019-09-07

はじめに

Googleカレンダーのようなカレンダーアプリはたくさんあります。
サービス間でカレンダーを共有したい場合があります。icsファイルを利用して簡単に別のカレンダーサービスにインポートできます。
Node.jsにてicsモジュールを利用すると簡単にicsファイルが作成できます。

icsとは

The iCalendar generator

URL:https://github.com/adamgibbons/ics

インストール

npm install -S ics

サイトにあるサンプル

const fs = require("fs");
const ics = require("ics")

const event = {
  start: [2018, 5, 30, 6, 30],
  duration: { hours: 6, minutes: 30 },
  title: 'Bolder Boulder',
  description: 'Annual 10-kilometer run in Boulder, Colorado',
  location: 'Folsom Field, University of Colorado (finish line)',
  url: 'http://www.bolderboulder.com/',
  geo: { lat: 40.0095, lon: 105.2669 },
  categories: ['10k races', 'Memorial Day Weekend', 'Boulder CO'],
  status: 'CONFIRMED',
  organizer: { name: 'Admin', email: 'Race@BolderBOULDER.com' },
  attendees: [
    { name: 'Adam Gibbons', email: 'adam@example.com', rsvp: true, partstat: 'ACCEPTED', role: 'REQ-PARTICIPANT' },
    { name: 'Brittany Seaton', email: 'brittany@example2.org', dir: 'https://linkedin.com/in/brittanyseaton', role: 'OPT-PARTICIPANT' }
  ]
}
 
ics.createEvent(event, (error, value) => {
  if (error) {
    console.log(error)
    return
  }
  //valueの値をxxx.icsファイルに出力
  ...
})

普通のYYYYMMDDHHmm形式の日本語時間を入れると9時間差が出てます。
日付の9をマイナスする処理を追加で対応できます。

const moment = require("moment-timezone");

moment.locale("ja");
let start = moment.tz("201909071800", "YYYYMMDDHHmm", "Asia/Tokyo");
start = start.add(-9, "hours");

const event = {
 start: [start.year(), start.month() + 1, start .date(), start.hours(), start.minute()],
 ...
}

また、一つicsファイルに複数イベントを作成することも可能です。

イベントオプション

https://github.com/adamgibbons/ics
のattributes部分を参照

以上

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?