Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JSON with list of national holidays Google Calendar

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.

like image 816
Vitor Guerreiro Avatar asked Mar 14 '23 13:03

Vitor Guerreiro


1 Answers

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.

like image 190
rphv Avatar answered Mar 22 '23 21:03

rphv