I'm creating event series on a Google Calendar based on information from Google Sheets linked to a Google Form. Dealing with dates is a mess and I'm getting erratic results.
I have a field on the spreadsheet with a start DATE shown as YYYY/MM/DD
I have a field with a start TIME shown as HH:mm
I have a field with an end TIME shown as HH:mm
I understand that internally the time has an year/month/day associated and the DATE also has a time associated that are not shown.
I will eventually need to create a calendar event series using the DATE part from the DATE and the TIME part from the time which I can change using setHours()
setMinutes()
methods.
Now my problem is creating a consistent Date object from the DATE cell Value as it seems to change weirdly.
var ssTZ = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var SSDate = ss.getRange(6,8).getValue();
var dataStart = Utilities.formatDate(SSDate, 'America/Brasilia' , 'MMMM dd, yyyy 12:00:00 Z');
var dataStartTZ = Utilities.formatDate(SSDate, ssTZ , 'MMMM dd, yyyy 12:00:00 Z');
var dataStartSP = Utilities.formatDate(SSDate, 'America/Sao_Paulo' , 'MMMM dd, yyyy 12:00:00 Z');
var dataStartOS = Utilities.formatDate(SSDate, 'GMT-3' , 'MMMM dd, yyyy 12:00:00 Z');
var date = new Date(dataStart);
var dateTZ = new Date(dataStartTZ);
var dateSP = new Date(dataStartSP);
var dateOS = new Date(dataStartOS);
Logger.log("Spreadsheet TimeZone: " + ssTZ);
Logger.log(SSDate);
Logger.log("");
Logger.log(date);
Logger.log(dateTZ);
Logger.log(dateSP);
Logger.log(dateOS);
That code produces the following Log outputs:
[19-07-02 20:39:49:780 BRT] Spreadsheet TimeZone: America/Sao_Paulo
[19-07-02 20:39:49:781 BRT] Tue Jan 12 00:00:00 GMT-02:00 2016
[19-07-02 20:39:49:782 BRT]
[19-07-02 20:39:49:784 BRT] Tue Jan 12 10:00:00 GMT-02:00 2016
[19-07-02 20:39:49:784 BRT] Tue Jan 12 12:00:00 GMT-02:00 2016
[19-07-02 20:39:49:785 BRT] Tue Jan 12 12:00:00 GMT-02:00 2016
[19-07-02 20:39:49:786 BRT] Mon Jan 11 13:00:00 GMT-02:00 2016
which don't make sense since all the timezones used should actually be the same one.
EDIT.... Addedinfo
As requested by @TheMaster the logs from the Text formatted variables are:
Logger.log(dataStart);
Logger.log(dataStartTZ);
Logger.log(dataStartSP);
Logger.log(dataStartOS);
[19-07-03 12:39:33:099 BRT] January 12, 2016 12:00:00 +0000
[19-07-03 12:39:33:100 BRT] January 12, 2016 12:00:00 -0200
[19-07-03 12:39:33:100 BRT] January 12, 2016 12:00:00 -0200
[19-07-03 12:39:33:100 BRT] January 11, 2016 12:00:00 -0300
The first 3 outputs as explained by @ziganotscha are due to summer time change to GMT-2 and America/Brasilia not beign recognized as valid timezone.
I'm still puzzled aout why it changes to January 11 when GMT-3 specified as Timezone.
Furthermore if i build the Date object directly from the cell value without formatting I get another diferent time:
var rawDate = new Date(SSDate);
19-07-03 12:39:33:101 BRT] rawDate: Tue Jan 12 2016 00:00:00 GMT-0200 (BRST)
I hate dealing with Dates... so confusing...
EDIT 2 ... More issues with time.
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Passeios");
var timeStart = ss.getRange(6,4).getValue();
var timeEnd = ss.getRange(6,5).getValue();
var ssTZ = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
Logger.log("timeStart: " + timeStart );
Logger.log("timeEnd: " + timeEnd);
var dateStart = ss.getRange(6,8).getValue();
var dateStartObj = new Date(Utilities.formatDate(dateStart, 'ssTZ' , 'MMMM dd, yyyy HH:mm:ss Z'));
var timeStartObj= new Date(Utilities.formatDate(timeStart, 'ssTZ' , 'MMMM dd, yyyy HH:mm:ss Z'));
var justTimeStart = Utilities.formatDate(timeStart, ssTZ, 'HH:mm');
Logger.log(" Time Start Object: " + timeStartObj);
Logger.log("Time Start Object Hours: " + timeStartObj.getHours());
Logger.log("Time Start Object Minutes: " + timeStartObj.getMinutes());
Logger.log("Start Time HH:mm: " + justTimeStart)
var hourStart = Utilities.formatDate(timeStart, ssTZ, 'HH');
var minutesStart = Utilities.formatDate(timeStart, ssTZ, 'mm');
var hourEnd = Utilities.formatDate(timeEnd, ssTZ, 'HH');
var minutesEnd = Utilities.formatDate(timeEnd, ssTZ, 'mm');
Logger.log(" TimeZone :" + ssTZ);
Logger.log(hourStart);
Logger.log(minutesStart);
Logger.log(hourEnd);
Logger.log(minutesEnd);
Produces the following log
timeStart: Sat Dec 30 1899 07:06:28 GMT-0300 (BRT)
timeEnd: Sat Dec 30 1899 07:36:28 GMT-0300 (BRT)
Time Start Object: Sat Dec 30 1899 07:06:28 GMT-0300 (BRT)
Time Start Object Hours: 7
Time Start Object Minutes: 6
Start Time HH:mm: 07:00
TimeZone :America/Sao_Paulo
07
00
07
30
The Spreadsheet cell is formatted as HH:mm and it shows 07:00 for start time 07:30 for end time
As you can see there is some 6 minutes and 28 seconds offset that I am not sure where is coming from when logging the cell value or when constructing a Date() object with the cell value.
Formating the cell to just Hours or just Minutes or HH:mm does not carry that offset.