Does anyone have an idea why this simple code does not work?:
var formattedSched = new Date(sched).setHours(15);
I am simply trying to add a time to a calendar event in google calendar through apps script. I am even using basically the same sample on the w3 page for set hours. http://www.w3schools.com/jsref/jsref_sethours.asp
But everytime I run the script to create an event in google calendar I get an error message that google thinks it is a number and not a date object. It works fine without the setHours() but with it, google thinks it's a number. Thanks
Update: Okay, I split out a test function to try and log what's happening:
function testsetcal() {
var formattedSched = new Date(); Logger.log(formattedSched.setHours(15))
CalendarApp.getDefaultCalendar()
.createEvent('Commo', formattedSched, formattedSched);
}
and the Logger result without setHours is: Wed Nov 21 08:12:33 PST 2012
Logger result with setHours is: 1.35352881266E12
which looks nothing like the example result on the w3 page.
Update: still not working and getting same logger result with setHours separated from main variable: 1.35352881266E12
function testsetcal() {
var formattedSched = new Date();
var withHours = formattedSched.setHours(15);
Logger.log(withHours);
CalendarApp.getDefaultCalendar()
.createEvent('Commo', withHours, withHours);
}
UPDATE: Okay wow - this is weird. I went to my google calendar to delete all the test events, at least just the ones I thought were successful. I then discovered that there were morte than I thought and some were set to 1500 - then I went back to the code and discovered that setHours() works on the outside of the google calendar create event like this:
function testsetcal() {
var formattedSched = new Date();
formattedSched.setHours(15);
Logger.log(formattedSched.setHours(15));
CalendarApp.getDefaultCalendar()
.createEvent('Commo', formattedSched, formattedSched);
}
So, this code set the hour to 1500, but that part of the code is to not be included in the creation of the event. Totally caught me surprised, but this seems to be the solution.