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 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);
Get your feed's name
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
Get your feed's prodid. Will always return a string.
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
Get current value of the CALSCALE
attribute. It will
return null
if no value was set. The iCal standard
specifies this as GREGORIAN
if no value is present.
Use this method to set your feed's CALSCALE
attribute. There is no
default value for this property and it will not appear in your iCal
file unless set. The iCal standard specifies this as GREGORIAN
if
no value is present.
cal.scale('gregorian');
Get current value of the SOURCE
attribute.
Use this method to set your feed's SOURCE
attribute.
This tells the client where to refresh your feed.
cal.source('http://example.com/my/original_source.ical');
SOURCE;VALUE=URI:http://example.com/my/original_source.ical
Get the current calendar timezone
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));
Get the current ttl duration in seconds
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
Get all custom X-* attributes.
Usually you get an ICalCalendar object like this:
But you can also use the constructor directly like this: