如何在JavaScript中获得时区名称?

我知道如何获得时区偏移,但我需要的是检测“美国/纽约”之类内容的能力。JavaScript能做到吗,还是我只能根据偏移量来估计?

188585 次浏览
你可以使用下面的映射表来简单地编写自己的代码: http://www.timeanddate.com/time/zones/ < / p >

或使用moment-timezone库: http://momentjs.com/timezone/docs/ < / p >

看到zone.name; // America/Los_Angeles

或,这个库: https://github.com/Canop/tzdetect.js < / p >

你可以使用这个脚本。 http://pellepim.bitbucket.org/jstz/ < / p >

在这里分叉或克隆存储库。 https://bitbucket.org/pellepim/jstimezonedetect < / p >

一旦你包含了这个脚本,你就可以得到- jstz.olson.timezones变量中的时区列表。

下面的代码用于确定客户端浏览器的时区。

var tz = jstz.determine();
tz.name();

享受jstz !

在javascript中,Date.getTimezoneOffset()方法返回当前地区相对于UTC的时区偏移,单位为分钟。

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

Moment'timezone将是一个有用的工具。 http://momentjs.com/timezone/ < / p >

在时区之间转换日期

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");


newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

国际化API支持获取用户时区,并且在当前所有浏览器中都是支持

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

Keep in mind that on some older browser versions that support the Internationalization API, the timeZone property is set to undefined rather than the user’s timezone string. As best as I can tell, at the time of writing (July 2017) all current browsers except for IE11 will return the user timezone as a string.

回答可能是获取时区的最佳方式,然而,Intl.DateTimeFormat().resolvedOptions().timeZone根据定义返回IANA时区名称,它是英文的。

如果你想要当前用户语言的时区名称,你可以从Date的字符串表示中解析它,如下所示:

function getTimezoneName() {
const today = new Date();
const short = today.toLocaleDateString(undefined);
const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });


// Trying to remove date from the string in a locale-agnostic way
const shortIndex = full.indexOf(short);
if (shortIndex >= 0) {
const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    

// by this time `trimmed` should be the timezone's name with some punctuation -
// trim it from both sides
return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');


} else {
// in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
return full;
}
}


console.log(getTimezoneName());

在Chrome和Firefox中测试。

当然,在某些环境中,这不会像预期的那样工作。例如,node.js返回一个GMT偏移量(例如GMT+07:00)而不是一个名称。但我认为作为备用方案还是有可读性的。

P.S.不会在IE11中工作,就像Intl...解决方案一样。

如果你已经在使用Moment.js,你可以猜出时区名称:

moment.tz.guess(); // eg. "America/New York"

这得到时区代码(例如,GMT)在旧的javascript(我使用谷歌应用程序脚本与旧引擎):

function getTimezoneName() {
return new Date().toString().get(/\((.+)\)/);
}

我把这个放在这里,以防有人需要。

用当前用户的语言表示结果的简短可能性:

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));

要检测像“;America/New York."”这样的内容,可以使用国际光子图书馆. 0中的new LocalZone()

import { LocalZone } from 'luxon';


const zoneName = new LocalZone().name;

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4).match(/\b(\w)/g).join(''))