Reputation: 85
I have a cell that's date formatted and set to: "Sat, Nov 2, 2019".
But when I look up that cells date with a script, it's one day off (-1).
var date = new Date();
date = sheet_log.getRange(110, 1).getValue();
var dateDisplay = sheet_log.getRange(110, 1).getDisplayValue();
// date = Fri Nov 01 2019 18:00:00 GMT-0600 (CST)
// dateDisplay = Sat, Nov 2, 2019
Why would Google Sheets let me set a date using a calendar picker, yet compute a date minus one day in script?
Is there any way around this?
Upvotes: 0
Views: 603
Reputation: 201388
How about this answer? Please think of this as just one of several possible answers.
Although I'm not sure about your actual condition, for example, the values of the timezone for the spreadsheet and the timezone for the script are different, such situation might occur.
getValue()
follows the timezone of the script.getDisplayValue()
follows the timezone of the Spreadsheet.The both timezones can be confirmed by the following script.
var timezone1 = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
var timezone2 = Session.getScriptTimeZone();
Logger.log(timezone1)
Logger.log(timezone2)
timezone1
is different from timezone2
, it is considered that the reason of the issue is due to the different timezone. In this case, please set the timezone of Spreadsheet or script.timezone1
is the same with timezone2
, it is considered that the reason of the issue is not due to the timezone. So it is required to investigate more for clarifying the reason of issue.If I misunderstood your situation, I apologize.
Upvotes: 1