You can pass options to set up your calendar or use setters to do this.
* import ical from 'ical-generator';
// or use require:
// const { default: ical } = require('ical-generator');
const cal = ical({name: 'my first iCal'});
// is the same as
const cal = ical().name('my first iCal');
// is the same as
const cal = ical();
cal.name('sebbo.net');
cal.toString()
would then produce the following string:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator//EN
NAME:sebbo.net
X-WR-CALNAME:sebbo.net
END:VCALENDAR
Calendar data
Creates a new ICalEvent and returns it. Use options to prefill the event's attributes. Calling this method without options will create an empty event.
import ical from 'ical-generator';
// or use require:
// const { default: ical } = require('ical-generator');
const cal = ical();
const event = cal.createEvent({summary: 'My Event'});
// overwrite event summary
event.summary('Your Event');
Returns all events of this calendar.
const cal = ical();
cal.events([
{
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
}
]);
cal.events(); // --> [ICalEvent]
Add multiple events to your calendar.
const cal = ical();
cal.events([
{
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
}
]);
cal.events(); // --> [ICalEvent]
Get the number of events added to your calendar
Get the feed method attribute. See ICalCalendarMethod for possible results.
Set the feed method attribute. See ICalCalendarMethod for available options.
import {ICalCalendarMethod} from 'ical-generator';
// METHOD:PUBLISH
calendar.method(ICalCalendarMethod.PUBLISH);
Set your feed's name. Is used to fill NAME
and X-WR-CALNAME
in your iCal file.
import ical from 'ical-generator';
const cal = ical();
cal.name('Next Arrivals');
cal.toString();
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator//EN
NAME:Next Arrivals
X-WR-CALNAME:Next Arrivals
END:VCALENDAR
Set your feed's prodid. prodid
can be either a
string like //sebbo.net//ical-generator//EN
or a
valid ICalCalendarProdIdData object. language
is optional and defaults to EN
.
cal.prodId({
company: 'My Company',
product: 'My Product',
language: 'EN' // optional, defaults to EN
});
cal.toString()
would then produce the following string:
PRODID:-//My Company//My Product//EN
Use this method to set your feed's timezone. Is used
to fill TIMEZONE-ID
and X-WR-TIMEZONE
in your iCal export.
Please not that all date values are treaded differently, if
a timezone was set. See formatDate for details. If no
time zone is specified, all information is output as UTC.
cal.timezone('America/New_York');
Sets the time zone to be used in this calendar file for all times of all
events. Please note that if the time zone is set, ical-generator assumes
that all times are already in the correct time zone. Alternatively, a
moment-timezone
or a Luxon object can be passed with setZone
,
ical-generator will then set the time zone itself.
For the best support of time zones, a VTimezone entry in the calendar is
recommended, which informs the client about the corresponding time zones
(daylight saving time, deviation from UTC, etc.). ical-generator
itself
does not have a time zone database, so an external generator is needed here.
A VTimezone generator is a function that takes a time zone as a string and returns a VTimezone component according to the ical standard. For example, ical-timezones can be used for this:
import ical from 'ical-generator';
import {getVtimezoneComponent} from '@touch4it/ical-timezones';
const cal = ical();
cal.timezone({
name: 'FOO',
generator: getVtimezoneComponent
});
cal.createEvent({
start: new Date(),
timezone: 'Europe/London'
});
Return a shallow copy of the calendar's options for JSON stringification. Third party objects like moment.js values or RRule objects are stringified as well. Can be used for persistence.
const cal = ical();
const json = JSON.stringify(cal);
// later: restore calendar data
cal = ical(JSON.parse(json));
Return generated calendar as a string.
const cal = ical();
console.log(cal.toString()); // → BEGIN:VCALENDAR…
Use this method to set your feed's time to live
(in seconds). Is used to fill REFRESH-INTERVAL
and
X-PUBLISHED-TTL
in your iCal.
const cal = ical().ttl(60 * 60 * 24); // 1 day
You can also pass a moment.js duration object. Zero, null
or negative numbers will reset the ttl
attribute.
Set X-* attributes. Woun't filter double attributes, which are also added by another method (e.g. busystatus), so these attributes may be inserted twice.
calendar.x([
{
key: "X-MY-CUSTOM-ATTR",
value: "1337!"
}
]);
calendar.x([
["X-MY-CUSTOM-ATTR", "1337!"]
]);
calendar.x({
"X-MY-CUSTOM-ATTR": "1337!"
});
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator//EN
X-MY-CUSTOM-ATTR:1337!
END:VCALENDAR
Set a X-* attribute. Woun't filter double attributes, which are also added by another method (e.g. busystatus), so these attributes may be inserted twice.
calendar.x("X-MY-CUSTOM-ATTR", "1337!");
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator//EN
X-MY-CUSTOM-ATTR:1337!
END:VCALENDAR
Usually you get an ICalCalendar object like this:
But you can also use the constructor directly like this: