Skip to content

Instantly share code, notes, and snippets.

@volgorean
Last active April 27, 2023 20:19
Show Gist options
  • Save volgorean/281196c8bb3e95d8410bf07b25452e7c to your computer and use it in GitHub Desktop.
Save volgorean/281196c8bb3e95d8410bf07b25452e7c to your computer and use it in GitHub Desktop.
Create calendar events.

Paste code into the browser command line. First the funciton definitions in create_events.js and press enter. Then contents of example_usage.js to create some events and press enter:

Screen Shot 2023-04-27 at 1 08 19 PM


Once you do that it'll download an ics file:

Screen Shot 2023-04-27 at 1 08 26 PM


which you can import into your google calendar like so:

Screen Shot 2023-04-27 at 1 08 40 PM

Screen Shot 2023-04-27 at 1 08 53 PM

Screen Shot 2023-04-27 at 1 09 23 PM

Screen Shot 2023-04-27 at 1 09 25 PM


here are the 2 example events in my calendar:

Screen Shot 2023-04-27 at 1 09 45 PM

function openICalEvents(events) {
let calendarData = "BEGIN:VCALENDAR\nVERSION:2.0\n";
events.forEach(function(event) {
calendarData += "BEGIN:VEVENT\n" +
"SUMMARY:" + event.title + "\n" +
"DTSTART:" + formatDate(event.startDate) + "\n" +
"DTEND:" + formatDate(event.endDate) + "\n" +
"LOCATION:" + event.location + "\n" +
"DESCRIPTION:" + event.description + "\n" +
"END:VEVENT\n";
});
calendarData += "END:VCALENDAR";
const calendarUrl = "data:text/calendar;charset=utf-8," + encodeURIComponent(calendarData);
window.open(calendarUrl);
}
function formatDate(date) {
const year = date.getUTCFullYear();
const month = padNumber(date.getUTCMonth() + 1);
const day = padNumber(date.getUTCDate());
const hours = padNumber(date.getUTCHours());
const minutes = padNumber(date.getUTCMinutes());
const seconds = padNumber(date.getUTCSeconds());
return year + month + day + "T" + hours + minutes + seconds + "Z";
}
function padNumber(num) {
return num < 10 ? "0" + num : num;
}
const events = [
{
title: "Meeting with John",
startDate: new Date("2023-05-01T10:00:00Z"),
endDate: new Date("2023-05-01T12:00:00Z"),
location: "Office",
description: "Discuss project status"
},
{
title: "Lunch with Jane",
startDate: new Date("2023-05-02T12:00:00Z"),
endDate: new Date("2023-05-02T13:00:00Z"),
location: "Restaurant",
description: "Catch up on recent events"
}
];
openICalEvents(events);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment