Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script formatDate using user's time zone instead of GMT

Tags:

In a function within Google Apps Script I am using Utilities.formatDate()

let formattedTimestamp = Utilities.formatDate(new Date(), "GMT", "hh:mm a");

The resulting string is a timestamp in the GMT time zone but I need it to be in the current user's time zone, and account for daylight saving time.

The documentation links to the Java SimpleDateFormat class documentation but I can't find a list of valid time zones with which I'd replace GMT.

like image 409
Employee Avatar asked Sep 03 '13 16:09

Employee


People also ask

How do I change the timeZone in Google App script?

New scripts default to the owner's time zone, but the script's time zone can be changed by clicking File > Project properties in the script editor. Note that spreadsheets have a separate time zone, which can be changed by clicking File > Spreadsheet settings in Google Sheets.

How do I change the date format in Google App script?

Use Utilities. formatDate(date, timeZone, format) to format a date object in this format.

How do I get the current date and time in Google script?

Get Current Date in Google Sheets Using TODAY To use the TODAY function to get the current date, type =TODAY() in the required cell. Similar to the NOW function, TODAY also does not require any parameters.

How do I find my timeZone in Appscript?

Step to get the timezone and locale: In manifest add useLocaleFromApp with true "useLocaleFromApp": true. Add the scope "https://www.googleapis.com/auth/script.locale" In buildAddOn you will receive the parameter userLocale and userTimezone.


1 Answers

You can directly get the spreadsheet Time Zone like this :

  var addedDate = sheet.getRange(1,1).getValue();
  var addedTime = Utilities.formatDate(addedDate, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "hh:mm a");

See doc here

but Google Apps Script uses these formats if you want to choose it yourself :

http://joda-time.sourceforge.net/timezones.html

like image 139
Serge insas Avatar answered Sep 27 '22 21:09

Serge insas