Home > Software engineering >  Google Scripts "Utilities.formatDate" does nothing with the time zone parameter
Google Scripts "Utilities.formatDate" does nothing with the time zone parameter

Time:01-27

This 2 lines generate the same output:

Logger.log(Utilities.formatDate(new Date(), "ET", "yyyy-MM-dd'T'HH:mm:ss'Z'"));
Logger.log(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'"));

Output:

2:20:37 PM Info -> 2022-01-26T19:20:37Z
2:20:37 PM Info -> 2022-01-26T19:20:37Z

Doesn't matter the input, is always converting the date to UTC (probably), this makes impossible to print the correct date and time.

What is the correct way to format a date-time in Google Scripts?

CodePudding user response:

"ET" is not an accepted time zone name.

For that you would use 'America/New_York' Apps Script is wacky that way.

Also, by putting the '' around the Z, it treats it as a string and just returns the date with a Z. I know that is what is in the example on the apps script page, but it is wrong.

Try this:

Logger.log(Utilities.formatDate(new Date(), "America/New_York", "yyyy-MM-dd'T'HH:mm:ss z"));
Logger.log(Utilities.formatDate(new Date(), "America/New_York", "yyyy-MM-dd'T'HH:mm:ss Z"));
Logger.log(Utilities.formatDate(new Date(), "America/Los_Angeles", "yyyy-MM-dd'T'HH:mm:ss z"));
Logger.log(Utilities.formatDate(new Date(), "America/Los_Angeles", "yyyy-MM-dd'T'HH:mm:ss Z"));
Logger.log(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss Z"));

Here is the list of all the timeZone ids

  •  Tags:  
  • Related