简体   繁体   中英

From Google Calendar API v2 to V3 - Javascript

I was simply develloping tool for getting event from google calendars in Javascript when i saw that the V2 of the API will be deprecated in less than a year. That clearly ruin my day ^^.

So i try to familiarise with this new API, but there is still something that I don't find. How to do the equivalent of the parameter "futureevents=true" with the javascript client of the google calendar API v3 ?

<script type="text/javascript">
    var apiKey = 'apikey';
    var scopes = 'https://www.googleapis.com/auth/calendar';

    function handleClientLoad() {
  gapi.client.setApiKey(apiKey);
  makeApiCall();
    }

    function makeApiCall() {
        gapi.client.load('calendar', 'v3', function() {
           var request = gapi.client.calendar.events.list({
               'calendarId': 'idcalendar@group.calendar.google.com'
            });
        request.execute(function(resp) {
           console.log(resp);
           for (var i = 0; i < resp.items.length; i++) {
               var title = document.createTextNode(resp.items[i].summary); 
               var description = document.createTextNode(resp.items[i].description + ' ');
               var location = document.createTextNode(resp.items[i].location + ' ');
               var date = document.createTextNode('Start: ' + resp.items[i].start.date + ' End: ' + resp.items[i].end.date);

               var div = document.createElement('div');
               var h1 = document.createElement('h1');
               h1.appendChild(title);
               div.appendChild(location);
               div.appendChild(description);
               div.appendChild(h1);
               var p = document.createElement('p');
               p.appendChild(date);
               div.appendChild(p);
               document.body.appendChild(h1);
               document.body.appendChild(div);
             }
          });
       }
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Here is my code, and with this, I get all the events of the calendar, how can i get only the one that didn't happend yet.

Thanks by advance

like Jay Lee said, but in order to use timeMin you also need to set 'singleEvents' to true. I also included a correct datetime string.

var request = gapi.client.calendar.events.list({
        'calendarId': 'idcalendar@group.calendar.google.com',
        'singleEvents': true, /* required to use timeMin */ 
        'timeMin': '2013-02-01T11:43:22.000Z'
    });

Use the timeMin parameter of the events.list() API call to request only events after now.

var request = gapi.client.calendar.events.list({
           'calendarId': 'idcalendar@group.calendar.google.com',
           'timeMin': '2013-05-09T09:43:00-04:00'
        });

you may need to play around with the Date format some, I'm not entirely sure what type the value should be but you'll just need to grab the current datetime and send it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM