支持嵌入式 Google 日历中的用户时区

Eng*_*ric 6 javascript google-calendar-api

Google 日历的嵌入代码在 URL 中插入了一个“ctz”变量以设置适当的显示时区,但是我正在与一个全球网站上的 500 多个用户合作,并试图让每个人都清楚日程安排(非付费语言学习者,否则我只会聘请专家)。

有没有人有关于如何根据用户的 IP 地址设置用户时区的建议?我已经在一天中相当广泛地浏览了这些问题。

小智 5

使用JSTZ库,例如通过 cdnjs 通过在类似中包含脚本标记

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.7/jstz.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在结束标记之前放置一个脚本标记:

  1. 使用 'calendar-container' 之类的 id 标记嵌入式日历 iframe 的容器
  2. 将 iframe 嵌入代码围绕 url 中的时区参数分成两段
  3. 从 JSTZ 获取时区名称
  4. 将容器的innerHTML 设置为重建的iframe 标记。

因此,如果您的日历嵌入 iframe 标签如下所示:

<iframe src="https://www.google.com/calendar/embed?showPrint=0&amp;showCalendars=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=somecalendaridentifier%40group.calendar.google.com&amp;color=%23AB8B00&amp;ctz=America%2FLos_Angeles" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>
Run Code Online (Sandbox Code Playgroud)

你的脚本看起来像:

<script type="text/javascript">
  var timezone = jstz.determine();
  var pref = '<iframe src="https://www.google.com/calendar/embed?showPrint=0&amp;showCalendars=0&amp;mode=WEEK&amp;height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=somecalendaridentifier%40group.calendar.google.com&amp;color=%23AB8B00&amp;ctz=';
  var suff = '" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>';
  var iframe_html = pref + timezone.name() + suff;
  document.getElementById('calendar-container').innerHTML = iframe_html;
</script>
Run Code Online (Sandbox Code Playgroud)


Eng*_*ric -1

有人建议采用名为JSTZ 的JavaScript 库并将其动态添加到 Google 日历的嵌入代码中。幸运的是,Google 日历似乎使用与浏览器相同的标准化 IANA 时区代码(好数据!),从而避免了大量的 SWITCH 语句。

var 时区 = jstz.define(); 时区.name(); “美国/纽约”

但我仍然不知道如何完成这件事。有人可以帮忙度过最后一英里吗?