0

My code displays a wrong date compared to the given parameters and i don't understand why ...

timezone = "Europe/Paris";
date = new Date(2017,11,31,15,01,0,0);
Logger.log(Utilities.formatDate(date, timezone, 'YYYY-MM-dd HH:mm'));

The result is:

[17-06-08 13:51:38:726 PDT] 2018-12-31 15:01

and should be:

[17-06-08 13:51:38:726 PDT] 2017-12-31 15:01

eleandar
  • 33
  • 4

1 Answers1

0

For reasons I cannot explain this seems to happen because you defined YYYY in capitals. Tried this in scripts:

  var timezone = "Europe/Paris";
  var date = new Date(2017,11,31,15,01,0,0);
  Logger.log(Utilities.formatDate(date, timezone, 'yyyy-MM-dd HH:mm'));

And outputs:

[17-06-08 23:01:26:459 CEST] 2017-12-31 15:01

Casper
  • 1,435
  • 10
  • 22
  • Have you tried replacing `date = new Date();` with `date = new Date(2017,11,31,15,01,0,0);` ? – eleandar Jun 08 '17 at 20:41
  • Isn't the month still wrong? Shouldn't it be: 2017-**11**-31 15:01. Only mentioning this, because I'm having the same strange behaviour, and a real final answer would be useful.. – vbenitogo Jul 13 '17 at 16:50
  • Took me some time but found [this SO question](https://stackoverflow.com/questions/12254333/javascript-is-creating-date-wrong-month). TLDR: month is zero-indexed, starting at 0 instead of 1. – Casper Jul 17 '17 at 07:38