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
.
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.
Use Utilities. formatDate(date, timeZone, format) to format a date object in this format.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With