Employee
Employee

Reputation: 2401

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

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.

Upvotes: 28

Views: 76802

Answers (1)

Serge insas
Serge insas

Reputation: 46792

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

Upvotes: 57

Related Questions