如何在 JavaScript 中获取浏览器时区 [英] How to get browser time zone in JavaScript

查看:60
本文介绍了如何在 JavaScript 中获取浏览器时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以 tz 数据库格式获取 JavaSript 中的用户时区,例如美国/洛杉矶?

Is it possible to get the user time zone in JavaSript in tz database format e.g. America/Los Angeles?

我注意到当我创建一个 Date 对象时,它的 toString 方法返回如下内容:

I noticed that when I create a Date object its toString method returns something like this:

2014 年 5 月 8 日星期四 08:40:48 GMT-0700(太平洋标准时间)

Thu May 08 2014 08:40:48 GMT-0700 (Pacific Standard Time)

所以也许可以将其更改为该地区的 tz 数据库中的 ID 之一(不幸的是,chrome 显示太平洋夏令时,因此似乎没有标准格式可显示).我需要一些库,因为我没有时间在诸如 firefox 与 chrome 之类的浏览器中使用所有可能的组合和差异来修正它.

so maybe it's possible to change it to one of ids in tz database from this region (unfortunately chrome shows Pacific Daylight Time so it seems like there's no standard format to display). I need some library because I don't have time to right it myself with all possible combination and discrepancies in browsers like firefox vs chrome.

推荐答案

我目前找到的最佳答案

var d = new Date()
var gmtHours = -d.getTimezoneOffset()/60;
console.log("The local time zone is: GMT " + gmtHours);

这篇关于如何在 JavaScript 中获取浏览器时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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