Skip to content

Instantly share code, notes, and snippets.

@tylershuster
Last active August 29, 2015 14:18
Show Gist options
  • Save tylershuster/e2333dc6f40175e761e7 to your computer and use it in GitHub Desktop.
Save tylershuster/e2333dc6f40175e761e7 to your computer and use it in GitHub Desktop.
Pulls in Calendar events and outputs in table
function pad2(number) {
return (number < 10 ? '0' : '') + number
}
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
$.get('https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events?orderBy=startTime&singleEvents=true&key={api_key}&orderBy=startTime',function(data){
var events = data.items;
console.log(events);
events.reverse();
for (var i = events.length - 1; i >= 0; i--) {
if(events[i].start.dateTime) {
var date = new Date(events[i].start.dateTime);
var time = {
hour: new Date(events[i].start.dateTime).getHours(),
minute: pad2(new Date(events[i].start.dateTime).getMinutes())
};
if(time.hour == 12) {
time = time.hour + ":" + time.minute + "<small>p.m.</small>";
} else if(time.hour > 12) {
time = (time.hour - 12) + ":" + time.minute + "<small>p.m.</small>";
} else {
time = time.hour + ":" + time.minute + "<small>a.m.</small>";
}
time = "at "+ time ;
} else {
var date = new Date(events[i].start.date);
var time = new Date(events[i].end.date);
console.log(date.getDate(),time.getDate()+1);
if(date.getDate() +1 == time.getDate()) {
time = "";
} else {
time = "to " + monthNames[time.getMonth()]+" "+time.getDate();
}
}
var month = date.getMonth();
var day = date.getDate();
var html = " <tr class='event'>\
<td><strong class='date'>"+monthNames[month]+" "+day+" "+time+"</strong></td>\
<td><span class='details'>"+events[i].summary+" - <a target='_blank' href='"+events[i].htmlLink+"'>(details)</a>";
if(events[i].location) {
html = html + "<br><span class='location'>"+events[i].location+"</span>";
}
html = html + " </span></td>\
</tr>";
$('#results').append(html);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment