-1

I have been struggling for the past a few hours by this weird behaviour from Google Sheet & Apps Script with date manipulation.

What I want to achieve & my current code is:

  1. Get current date & time
var nowtime = new Date().getTime(); 
var nowdate = new Date(nowtime);
var formattedNow = Utilities.formatDate(nowdate, "Australia/Sydney", "dd/MM/yyyy HH:mm");

Logger.log(nowtime);
Logger.log(nowdate);
Logger.log(formattedNow);

Result:

1.659269747206E12
Sun Jul 31 22:15:47 GMT+10:00 2022
31/07/2022 22:15
  1. Get a date & time forward 10 days
var thentime = nowtime + 1000 * 60 * 60 * 24 * 10; //that is 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day and 10 days 
var thendate = new Date(thentime);
var formattedThen = Utilities.formatDate(thendate, "Australia/Sydney", "dd/MM/yyyy HH:mm");

Logger.log(thentime);
Logger.log(thendate);
Logger.log(formattedThen);

Result:

1.660133747212E12
Wed Aug 10 22:15:47 GMT+10:00 2022
10/08/2022 22:15

Everything above to this point is all correct, now the weird behaviours:

  1. Record both date/times in a corresponding cell
sheet.getRange("A1").setValue(formattedNow);
sheet.getRange("A2").setValue(formattedThen);

Value in cell A1 in the Google Sheet is

31/07/2022 22:15

But value in cell A2 in the Google Sheet is

10/8/2022 22:15:00

So cell A2 is not following the Utilities.formatDate already.

And when I try to extract the date value from both A1 & A2 by using following method:

var a1 = new Date(sheet.getRange("A1").getValue());
var a1date = Utilities.formatDate(a1, "Australia/Sydney", "dd/MM/yyyy HH:mm");
Logger.log(a1date);

var a2 = new Date(sheet.getRange("A2").getValue());
var a2date = Utilities.formatDate(a2, "Australia/Sydney", "dd/MM/yyyy HH:mm");
Logger.log(a2date);

Results are:

31/07/2022 22:15 //A1, correct
09/10/2022 09:15 //A2, INCORRECT

I thought it's maybe related to the issue described in this question: Google Apps Script date format issue (Utilities.formatDate) , but it's not really working, and there is something even more weird:

If I change the date manipulation to a number less than 1 day in milliseconds, for example, instead of doing

var thentime = nowtime + 1000 * 60 * 60 * 24 * 10

but to do this:

var thentime = nowtime + 1000 * 60 * 60 * 23 

All the outcomes would then be completely correct and as expected.

Then I did some more research on date manipulation thinking maybe I was doing the wrong way, I tried Google App Script Utilities.formatDate(new Date() adding days to today and it's still giving me the same issue.

---- Update 01/08/2022 ---- Below are complete code to reproduce the issue:

function onOpen(e) {

  const gsId = "Google Sheet ID"
  const gs = SpreadsheetApp.openById(gsId)
  const sheet = gs.getSheetByName("Sheet1")

  var nowtime = new Date().getTime();
  var nowdate = new Date(nowtime);
  var formattedNow = Utilities.formatDate(nowdate, "Australia/Sydney", "dd/MM/yyyy HH:mm");
  Logger.log(nowtime);
  Logger.log(nowdate);
  Logger.log(formattedNow);

  var thentime = new Date().getTime();
  thentime = thentime + 1000 * 60 * 60 * 24 * 10;
  var thendate = new Date(thentime);
  var formattedThen = Utilities.formatDate(thendate, "Australia/Sydney", "dd/MM/yyyy HH:mm");
  Logger.log(thentime);
  Logger.log(thendate);
  Logger.log(formattedThen);

  Logger.log("----everything above is showing correct results----");

  sheet.getRange("A1").setValue(formattedNow);
  sheet.getRange("A2").setValue(formattedThen);
  
  var a1 = new Date(sheet.getRange("A1").getValue());
  var a1date = Utilities.formatDate(a1, "Australia/Sydney", "dd/MM/yyyy HH:mm");
  Logger.log(a1date);

  var a2 = new Date(sheet.getRange("A2").getValue());
  var a2date = Utilities.formatDate(a2, "Australia/Sydney", "dd/MM/yyyy HH:mm");
  Logger.log(a2date);

}
aZgauAL9
  • 1
  • 1
  • What is the timezone of you spreadsheet? – Rubén Jul 31 '22 at 17:16
  • @Rubén In *appsscript.json* the timezone setting is `"timeZone": "Australia/Sydney"`. And when I `Logger.log(Session.getScriptTimeZone())` it gives me the same result. It's not an issue with spreadsheet timezone I'm afraid. – aZgauAL9 Jul 31 '22 at 22:57
  • To get the spreadsheet time zone, in Google Sheets open the spreadsheet, then click on File > Settings or in Google Apps Script use SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(). Sometime the spreadsheet time zone is not exactly the same of the script. – Rubén Jul 31 '22 at 23:29
  • @Rubén *appsscript.json* is the same as timezone setting in File > Settings. Also `SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone()` shows `Australia/Sydney` as well. – aZgauAL9 Jul 31 '22 at 23:35
  • I'm not getting the same logs as you for `var a1 = new Date(sheet.getRange("A1").getValue()); var a1date = Utilities.formatDate(a1, "Australia/Sydney", "dd/MM/yyyy HH:mm"); Logger.log(a1date); var a2 = new Date(sheet.getRange("A2").getValue()); var a2date = Utilities.formatDate(a2, "Australia/Sydney", "dd/MM/yyyy HH:mm"); Logger.log(a1date);` Please edit the question to add a [mcve] (please make the question concise, include Spreadsheet locale and time zone settings, A1 and A2 cell formatting) – Rubén Jul 31 '22 at 23:49
  • @Rubén I've update the question with complete code to reproduce the issue. Spreadsheet locale and time zone settings are both "Australia/Sydney" which can be checked by calling `SpreadsheetApp.getActive().getSpreadsheetTimeZone()` or seen in both *appsscript.json* file and Google Sheet File > Settings. – aZgauAL9 Aug 01 '22 at 00:06
  • Not a Google Sheets expert by any stretch, but… why assign formatted strings to the cells? Surely you should be assigning date objects and let the cell formatting do the rest. If you write strings, likely the spreadsheet then parses them back to dates, which is fraught I would think. – RobG Aug 01 '22 at 03:28
  • @RobG Thanks and I think you are spot on. The reason I pass formatted strings to the cells are just for aesthetics reason, to show uniformed results. I will try to pass date values directly to cells. – aZgauAL9 Aug 01 '22 at 04:51

1 Answers1

0

Get time and date

function getDateAndTime() {
  const dt = new Date();
  var nowtime = Utilities.formatDate(dt,SpreadsheetApp.getActive().getSpreadsheetTimeZone(),"HH:mm:ss");
  var nowdate = dt;
  var formattedNow = Utilities.formatDate(nowdate, "SpreadsheetApp.getActive().getSpreadsheetTimeZone()", "dd/MM/yyyy HH:mm");
  Logger.log(nowtime);
  Logger.log(nowdate);
  Logger.log(formattedNow);
}

When using Date methods always refer to document to insure that you understand what is returned.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • First of all thank you! It seems to be a timezone issue, but the thing is: 1. Trying your script it throws wrong answers (`formattedNow` is showing GMT time but both `nowdate` and `nowtime` are correct): `09:00:14` / `Mon Aug 01 09:00:14 GMT+10:00 2022 9:00:14 AM` and `31/07/2022 23:00` 2. In *appsscript.json* the timezone setting is `"timeZone": "Australia/Sydney"`. And when I `Logger.log(Session.getScriptTimeZone())` it gives me the same result. Everything seems correct. – aZgauAL9 Jul 31 '22 at 23:01
  • 3. In my original question, only by changing the date manipulation part will simply give the correct answer (in both correct format and value), which is super weird. – aZgauAL9 Jul 31 '22 at 23:07
  • Given `const dt = new Date()`, the use of `var nowdate = dt` seems redundant given both *dt* and *nowdate* reference the same *Date* instance. – RobG Aug 01 '22 at 03:42
  • @RobG It depends a lot on how the data in stored in the spreadsheet. I know it can be redundant with the data is a Date() object but when the data is a string then it works also and provides the necessary Date() object. – Cooper Aug 01 '22 at 14:57