首页 > 解决方案 > 从 NodeJS 中的国家和州获取时区

问题描述

因此,从国家这样的输入:美国和州:CA我需要获取时区。

我在这里看到了这个回复:Get timezone directly from Country and State Google API,但问题是 Google API 需要经度和纬度,所以我想知道我是否首先需要查找这些值。有没有直接获取时区的选项?

谢谢

标签: node.jstimezonelocation

解决方案


Moment.js gives you some functionality to do this

I have installed it with npm install moment-timezone

Then you can get the TimeZone as follows here for California using Los_Angeles

var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();

var a = moment.tz("2013-11-18 11:55", "America/Los_Angeles");
var b = moment.tz("2013-11-18 11:55", "America/Toronto");



console.log(a.format()); // 2013-11-18T11:55:00-08:00
console.log(b.format()); // 2013-11-18T11:55:00-05:00

console.log(a.utc().format()); // 2013-11-18T19:55:00Z
console.log(b.utc().format()); // 2013-11-18T16:55:00Z

推荐阅读