I have been using Session.getTimeZone()
to get the current user's timezone. I just noticed it has been deprecated, and I can't see anything that replicates this functionality. My users are located all over the US and dates/times should be formatted accordingly. How can I get the current user's timezone and not that of the script/owner?
The project in question is a standalone script.
Greg, normal javascript seems to be working as intended this might be new in gas. The second example of code's function is returning the correct timezone offset for me.
I'm currently using the Session.getTimeZone() in a gasOffsetScript.
Question here might be a good read for the future.
Using Calendar Timezone to set event Timezone, not the users
This next code will get the users timezone based off their default Calendar Setting, if the script is running as the user.
function getUserTimeZone() {
var userTimeZone = CalendarApp.getDefaultCalendar().getTimeZone();
Logger.log(userTimeZone)
}
Or you could modify the following using normal javascript.
Code that is returning users timezone Stackoverflow question
function userTimeZone() {
function pad(number, length){
var str = "" + number
while (str.length < length) {
str = '0'+str
}
return str
}
var offset = new Date().getTimezoneOffset()
offset = ((offset<0? '+':'-')+ // Note the reversed sign!
pad(parseInt(Math.abs(offset/60)), 2)+
pad(Math.abs(offset%60), 2))
Logger.log(offset)
}
Good luck brother, timezones are thorns.
In Gmail Add-ons you can get the user timezone and locale in buildAddOn parameters by activating the useLocaleFromApp. Check the documentation of Accessing User Locale and Timezone
Step to get the timezone and locale:
"useLocaleFromApp": true
Manifest example:
{
"timeZone": "Etc/GMT"
"oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/gmail.addons.current.message.readonly", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/script.locale"],
"gmail": {
"name": "TestApp",
"logoUrl": "https://storage.googleapis.com/xxxxxxx/favicon.ico",
"contextualTriggers": [{
"unconditional": {
},
"onTriggerFunction": "buildAddOn"
}],
"primaryColor": "#fea001",
"secondaryColor": "#fea001",
"openLinkUrlPrefixes": ["https://mail.google.com/"],
"version": "TRUSTED_TESTER_V2",
"useLocaleFromApp": true
}
}
Add-ons example:
function buildAddOn(e)
{
var cards = [];
var card = CardService.newCardBuilder().setName("Test timeZone");
card.setHeader(CardService.newCardHeader().setTitle("test"));
var section = CardService.newCardSection();
section.addWidget( CardService.newTextParagraph().setText(e.userLocale));
section.addWidget( CardService.newTextParagraph().setText(e.userTimezone.offSet));
section.addWidget( CardService.newTextParagraph().setText(e.userTimezone.id));
card.addSection(section);
cards.push( card.build());
return cards;
}
Caution with userTimezone.offSet parameter. The parameter have distinct case that the referenced in the documentation. The parameter is userTimezone.offSet with S uppercase
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