Skip to content

Instantly share code, notes, and snippets.

@4kills
Last active October 13, 2020 07:22
Show Gist options
  • Save 4kills/0bd390c817a331d41ab12d346f580d5d to your computer and use it in GitHub Desktop.
Save 4kills/0bd390c817a331d41ab12d346f580d5d to your computer and use it in GitHub Desktop.
Creates an ics birthday calendar from a space separated text file (bdays_src.txt [same dir]) formatted like: [first name] [last name] [DD.MM.YYYY][os.EOL]
const fs = require('fs')
const os = require('os')
const dateToIcsString = date => "" + date.getFullYear() + (date.getMonth()+1).toString().padStart(2, '0') + date.getDate().toString().padStart(2,'0');
var f
try {
f = fs.readFileSync('bdays_src.txt', 'utf8')
} catch (err) {
console.error(err)
}
var data = f.split(os.EOL)
data = data.map(v => v.split(' '))
var bdays = ""
var id = 0
data.forEach(v => {
var date = new Date(v[2].split(".").reverse().join("-"))
var dateEnd = new Date(date)
dateEnd.setDate(dateEnd.getDate() + 1)
const lastName = v[1]
const icsEventTemplate = `BEGIN:VEVENT
DTSTART;VALUE=DATE:${dateToIcsString(date)}
DTEND;VALUE=DATE:${dateToIcsString(dateEnd)}
RRULE:FREQ=YEARLY
DTSTAMP:20201012T095216Z
UID:587id060i821ren432uo8rklt4${id++}@google.com
CREATED:20201012T095002Z
DESCRIPTION:
LAST-MODIFIED:20201012T095002Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:${v[0]} ${lastName}${lastName[lastName.length-1]=='s' ? '\'' : '\'s'} Birthday
TRANSP:TRANSPARENT
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:This is an event reminder
TRIGGER:-P0DT19H0M0S
END:VALARM
END:VEVENT`
bdays += icsEventTemplate+os.EOL
})
const out = `BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:SAP Birthdays
X-WR-TIMEZONE:Europe/Berlin
X-WR-CALDESC:Birthdays of Colleagues at SAP
${bdays}END:VCALENDAR
`
fs.writeFile('birthdays.ics', out, (err)=>{if (err) console.log(err)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment