I have this script which is to populate a google calendar with an event. I have a date field and a start and finish time field. While in the google spreadsheet the start time is
The problem is simple but the explanation is less simple...
Let us try anyway !
code modification :
var tstart = new Date(row[3]);
Logger.log('hour only = '+tstart)
tstart.setDate(date.getDate());
Logger.log('setDate = '+tstart)
tstart.setMonth(date.getMonth());
Logger.log('setMonth = '+tstart)
tstart.setYear(date.getYear());
Logger.log('setYear = '+tstart)
As you see, I added a couple of Logger.log
in your code and notice a difference in time value like shown below
[14-07-28 16:18:53:441 BST] hour only = Sat Dec 30 1899 10:00:00 GMT-0000 (GMT)
[14-07-28 16:18:53:442 BST] setDate = Fri Dec 29 1899 10:00:00 GMT-0000 (GMT)
[14-07-28 16:18:53:442 BST] setMonth = Sat Jul 29 1899 10:00:00 GMT+0100 (BST)
[14-07-28 16:18:53:442 BST] setYear = Tue Jul 29 2014 10:00:00 GMT+0100 (BST)
When you create the date object with hours only, the date is logically december 30 1899 which is in winter.(december 30 1899 is the "epoch" used as reference in JS date)
Then you set the date (still in december) and then the month which is July and July is obviously in summer... as you can see in the logger, the date object has switched from GMT to BST (british summer time I guess ?) and from there it is 1 hour later.
To solve that you have a few possibilities, the simplest one would be to combine time and date in the spreadsheet itself, this would create a complete date object that wouldn't need any change.
If you can't (or don't want) to change your SS layout I guess the easier way would be to convert your time value to a string using Utilities.formatDate()
and parse this string into integer values to get hours and minutes.
You'll have to change a bit the sequence order of your date manipulations but from what I see of your coding skills that should not be a problem.
Hoping my explanation was clear enough ;)