Rafa Rafa - 1 year ago 67
Javascript Question

getEventsForDay() formatting issues with datepicker return value

I'm trying to use the Calendar API to pull up some events based on datepicker output. The issue that i'm facing with my AppScript is formatting properly the value that i get from the datepicker that will serve as input for the getEventsForDay() function.

function testing(){
var z = CalendarApp.getCalendarsByName("X")[0];
var date = new Date('2016-10-12');
var dateformatted =Utilities.formatDate(date), "GMT", "yyyy-MM-dd hh:mm:ss a");
var a = z.getEventsForDay(dateformatted, {search: 'OOO'});

The output of a into this scenario is a empty object - which is expected because this formatting is not working at all. (i've read 1000 posts that this should work).

For context as well, i have one working example with today's date, which it works fine becase the input is a new Date(). Here you go:

var datetoday = new Date();
var b = z.getEventsForDay(datetoday, {search: 'OOO'});

Any ideas on what i'm missing here?

Thanks in advance.

Answer Source

Since new Date() returns the Date object in UTC and not the local timezone, you may be querying the wrong day and hence the empty object if the other day does not have any events.

You can covert the date to the current timezone like

date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000)

This should return the date in the local timezone and you will get the events for the correct day.

Hope it helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download