Last active
December 18, 2017 06:38
-
-
Save pouyakary/04356653cd3c52e815c6c665bca0b0b6 to your computer and use it in GitHub Desktop.
To create a link for Google Calendar events
This file contains 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
/** | |
* createGoogleCalendarLink accepts an input object in this form: | |
* ``` | |
* { | |
* title?: "string", | |
* details?: "string", | |
* location?: "string", | |
* | |
* start: { | |
* year: string, day: string, month: string, | |
* hour: string, minute: string | |
* }, | |
* | |
* end: { | |
* year: string, day: string, month: string, | |
* hour: string, minute: string | |
* }, | |
* } | |
* ``` | |
* | |
* Not that `?` is for the optional ones | |
* | |
* @param {object} input | |
*/ | |
function createGoogleCalendarLink ( input ) { | |
// toolings | |
const baseURL = | |
"https://www.google.com/calendar/render?action=TEMPLATE?" | |
const arguments = [ ] | |
const append = ( key, value ) => | |
arguments.push( key + "=" + encodeURIComponent( value ) ) | |
const encodeDate = ( obj ) => | |
obj.year + obj.month + obj.day + "T" + obj.hour + obj.minute + "00Z" | |
// optionals | |
if ( input.title ) append( "title", input.title ) | |
if ( input.details ) append( "details", input.details ) | |
if ( input.location ) append( "location", input.location ) | |
// main dates | |
append( "dates", | |
encodeDate( input.start ) + "/" + encodeDate( input.end ) ) | |
// and done | |
return baseURL + arguments.join( "&" ) | |
} | |
// example | |
console.log( createGoogleCalendarLink({ | |
title: "something to have an event for", | |
// details: "stuff, blah blah", | |
location: "virgo supercluster", | |
start: { | |
year: "2017", day: "18", month: "11", | |
hour: "20", minute: "18" | |
}, | |
end: { | |
year: "2017", day: "19", month: "11", | |
hour: "20", minute: "10" | |
} | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example outputs: