Last active
December 5, 2022 18:57
-
-
Save brunos3d/225650ab359f6fa3d570c7e1eee35a22 to your computer and use it in GitHub Desktop.
Generate Add to Google Calendar URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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