Skip to content

Instantly share code, notes, and snippets.

@brunos3d
Last active December 5, 2022 18:57
Show Gist options
  • Save brunos3d/225650ab359f6fa3d570c7e1eee35a22 to your computer and use it in GitHub Desktop.
Save brunos3d/225650ab359f6fa3d570c7e1eee35a22 to your computer and use it in GitHub Desktop.
Generate Add to Google Calendar URL
// this file is just to set the name of the gist, use just one of the functions below
export * as withLuxon from './withLuxon'
export * as withMoment from './withMoment'
export * as withDateFns from './withDateFns'
import { format, parseISO } from 'date-fns';
export function generateCalendarUrl(
startDatetime: string,
endDatetime: string,
title: string,
description: string,
timezone = `America/Sao_Paulo`,
) {
const dateTimeFormat = `yyyyMMdd'T'HHmmssxxx`;
const startMoment = parseISO(startDatetime);
const endMoment = parseISO(endDatetime);
const calendarInviteUrl = `https://calendar.google.com/calendar/render?action=TEMPLATE&dates=${format(
startMoment,
dateTimeFormat,
)}/${format(endMoment, dateTimeFormat)}${
timezone && `&ctz=${timezone}`
}&text=${encodeURI(title)}&details=${encodeURI(description)}`;
return calendarInviteUrl;
}
import { DateTime } from 'luxon';
export function generateCalendarUrl(startDatetime: string, endDatetime: string, title: string, description: string, timezone = `America/Sao_Paulo`) {
const dateTimeFormat = `yyyyMMdd'T'HHmmssZZ`;
const startMoment = DateTime.fromISO(startDatetime).setZone(timezone, { keepLocalTime: true });
const endMoment = DateTime.fromISO(endDatetime).setZone(timezone, { keepLocalTime: true });
const calendarInviteUrl = `https://calendar.google.com/calendar/render?action=TEMPLATE&dates=${startMoment.toFormat(
dateTimeFormat
)}/${endMoment.toFormat(dateTimeFormat)}${timezone && `&ctz=${timezone}`}&text=${encodeURI(title)}&details=${encodeURI(description)}`;
return calendarInviteUrl;
}
import moment from 'moment-timezone';
type DateType = Date | string | number;
export function generateCalendarUrl(
startDatetime: DateType,
endDatetime: DateType,
title: string,
description: string,
timezone = `America/Sao_Paulo`,
) {
const dateTimeFormat = `YYYYMMDDTHHmmssZ`;
const startMoment = moment.tz(startDatetime, timezone);
const endMoment = moment.tz(endDatetime, timezone);
const calendarInviteUrl = `https://calendar.google.com/calendar/render?action=TEMPLATE&dates=${startMoment.format(
dateTimeFormat,
)}/${endMoment.format(dateTimeFormat)}${
timezone && `&ctz=${timezone}`
}&text=${encodeURI(title)}&details=${encodeURI(description)}`;
return calendarInviteUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment