获取当前用户时区 [英] Getting current user timezone

查看:155
本文介绍了获取当前用户时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用 Session.getTimeZone()来获取当前用户的时区。我刚刚注意到它已被弃用,我可以看不到任何复制此功能的东西。我的用户遍布美国,日期/时间应该相应地进行格式化。如何获取当前用户的时区而不是脚本/所有者?



有问题的项目是一个独立的脚本。


<格雷格,正常的JavaScript似乎正在按预期工作,这可能是新的气体。代码函数的第二个例子是为我返回正确的时区偏移量。
我目前在gasOffsetScript中使用Session.getTimeZone()。
这里的问题可能是对未来的一个很好的阅读。



接下来的代码将得到用户的时区基于它们的默认日历设置,如果脚本以用户身份运行。

  function getUserTimeZone(){
var userTimeZone = CalendarApp.getDefaultCalendar()。getTimeZone();
Logger.log(userTimeZone)
}

或者您可以修改以下使用正常的JavaScript。



返回用户时区的代码
Stackoverflow问题

  function userTimeZone(){
function pad (数字,长度){
var str =+数字
while(str.length< length){
str ='0'+ str
}
返回str
}

var offset = new Date()。getTimezoneOffset()
offset =((offset< 0?'+':' - ')+ //注意反转的符号!
pad(parseInt(Math.abs(offset / 60)),2)+
pad(Math.abs(offset%60),2))
Logger。日志(抵消)
}

祝你好运,兄弟时区都是荆棘。


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.

这篇关于获取当前用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆