4

How to get a holiday list of selected country using Google Calendar with javascript or jquery?

It's possible? Any suggestion?

I'm working with Google Calendar API V3 and using javascript. I found some examples in android, but I don't know nothing about android.

Vitor
  • 523
  • 2
  • 4
  • 28
  • Already answered: http://stackoverflow.com/questions/18996577/how-to-get-national-holidays-of-selected-country – Arius Oct 08 '15 at 14:02
  • I need javascript example, not android. @Arius – Vitor Oct 08 '15 at 14:06
  • Sorry but what is the difference? You are using API in the same way in all languages... – Arius Oct 08 '15 at 14:07
  • No, it is not same way. See at: https://developers.google.com/google-apps/calendar/. Have many ways to start as differents languages. – Vitor Oct 08 '15 at 14:13
  • https://developers.google.com/google-apps/calendar/v3/reference/ - this is just REST API to consume. Despite the differences of conusming API like this in different languages, you will ALWAYS ask the same endpoints with the same results no matter what language you will use. You can just use $.ajax() call from jQuery to receive results. – Arius Oct 08 '15 at 14:49
  • you could solve it programmatic using the 'Easter Algorithm': http://math.stackexchange.com/questions/896954/decoding-gauss-easter-algorithm – Christian Oct 08 '15 at 14:50
  • Anyway @VitorGuerreiro http://stackoverflow.com/questions/13651017/accessing-google-calendar-api-with-javascript-without-authorization – Arius Oct 08 '15 at 14:51
  • None of these answers shows what I need, I will keep trying here. Thanks anyway. – Vitor Oct 08 '15 at 15:43

1 Answers1

7

Here's a simple example with a handful of the public holidays calendars:

$("#selectCountry").change(function(e) {
  $("#output").html("Loading...");
  var country = $("#selectCountry").val();
  var calendarUrl = 'https://www.googleapis.com/calendar/v3/calendars/en.' + country 
                  + '%23holiday%40group.v.calendar.google.com/events?key=<yourAPIKey>';

  $.getJSON(calendarUrl)
    .success(function(data) {
     console.log(data);
      $("#output").empty();
      for (item in data.items) {
        $("#output").append(
          "<hr><h3>" + data.items[item].summary + "<h3>" +
          "<h4>" + data.items[item].start.date + "<h4>"
        );
      }
    })
    .error(function(error) {
      $("#output").html("An error occurred.");
    })
});
$("#selectCountry").trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <select id="selectCountry">
    <option value="usa">USA</option>
    <option value="uk">UK</option>
    <option value="bm">Bermuda</option>
    <option value="swedish">Sweden</option>
  </select>
  <pre id="output"></pre>
  <script type="text/javascript">
  </script>
</body>

This strategy constructs a URL for the calendar from the option selected in the dropdown menu. It parses the holiday name and information from the raw JSON response, which is contained in the data parameter of the $.getJSON callback function.

rphv
  • 5,409
  • 3
  • 29
  • 47
  • 2
    Yes, the [V2 interface was deprecated](https://developers.google.com/gdata/samples/cal_sample) in November of 2014, and my original answer used that interface. I've updated the answer to use the V3 interface, but note that this requires you to use an [API key as detailed in this answer](http://stackoverflow.com/a/27213635/1612562). Just replace `` in the code above with a valid browser API key. – rphv May 18 '16 at 18:43
  • how i will get the data year wise – amol challawar Apr 17 '17 at 05:43
  • This doesn't work for all the countries, try TR, DE, FR and you will get an error – Sodruldeen Mustapha Oct 22 '21 at 12:15