2

On my Google Sheets macro, I want it to get the first date of the previous month, and insert it into a cell.

  var currentDate = new Date();
  var year = currentDate.getFullYear();
  var month = currentDate.getMonth() - 1;
  var lastMonthStart = new Date(year, month, 1);
Logger.log(lastMonthStart)
 dataDashboard.getRange(1, 1).setValue(lastMonthStart)

When I run this, the correct date is recorded in the logger:

[19-10-18 10:51:38:454 PDT] Sun Sep 01 00:00:00 GMT+01:00 2019

but in the sheet, it puts 31/08/2019 16:00:00

I'm guessing this is something to do with timezones, and maybe wherever the macro is running has a different timezone to the sheet and is trying to be helpful and correct it. How can I stop this?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Rusty
  • 609
  • 1
  • 4
  • 19
  • 3
    The spreadsheet and the script project have their own timezone setting. The easier solution is to make both files to use the same timezone. – Rubén Oct 18 '19 at 18:22
  • 1
    Ruben's suggestion is correct. You can take a look at [this answser](https://stackoverflow.com/a/18597149/11581830) for an example on how to format the timezone information. – AMolina Oct 21 '19 at 10:33

0 Answers0