As a freelancer I like my (potential) clients to be aware of my availability. I decided to build something that would allow me to fill in my availability in Google Calendar, which would then be displayed in a nice widget on the website.

The widget doesn’t need to display anything but whether or not I am available or busy on a certain day. I figured that the good ol’ jQuery Datepicker would do perfectly.

So here’s what I did.

1. Get an API Key.

When you google for accessing the Google API, most forums will point you in the direction of the OAUTH2 protocol. Using OAuth would be necessary if we wanted to access restricted content. For accessing publicly accessible content, the Google API is a lot easier. All you need is an URL and an API key.

Go to and create a new project. Select credentials and create an API Key. You can restrict the use of the key although you don’t have to. Copy the API key value somewhere: we’ll need it in our code.

2. Create a public calendar.

alt text;

Create a new calendar in your google calendar and set it to public.

alt text

Copy the Calendar ID, we’ll need it later.

In my case I created two calendars, one to display the dates that I am available, and the other for my ‘busy calendar’.

3. Add the following HTML to your page.

<div id="datepicker"></div>

And add the jQuery dependencies to the HEAD of your page.

<script src=""></script>
<script src=""></script>

4. Add the following Javascript to your page.

$(document).ready(function () {
    numberOfMonths: [3, 1],
    onChangeMonthYear: function () {
    onSelect: function(){


  function setCalendar() {
    var api_key = '<GOOGLE API KEY>';
    var calendars = {
      free: '<CALENDAR_ID_FREE>',
      busy: '<CALENDAR_ID_BUSY>',
    $.each(calendars, function (type, calendar_id) {
        url: "" + calendar_id + "/events?key=" + api_key,
        success: function (data) {
          data.items.forEach(function (item) {
            var start = new Date(;
            var end = new Date(;
            do {
              $('td[data-year="' + start.getFullYear() + '"][data-month="' + start.getMonth() + '"] a').filter(function () {
                return $(this).text() == start.getDate();
              start.setDate(start.getDate() + 1);
            } while (start.getTime() !== end.getTime());

Fill in the two calendar IDS and your API Key. The script basically fetches the events of one ‘busy’ calendar I created and one ‘free’ calendar, and adds classes to our jQuery widget for both.

5. Add the necessary CSS.

In my case I chose to render the DatePicker widget without any shipped CSS: I just added the necessary styling myself.

This is the result:

comments powered by Disqus