我知道如何获得时区偏移,但我需要的是检测“美国/纽约”之类内容的能力。JavaScript能做到吗,还是我只能根据偏移量来估计?
当前回答
你可以使用下面的映射表来编写自己的代码: http://www.timeanddate.com/time/zones/
或者,使用moment-timezone库: http://momentjs.com/timezone/docs/
看到zone.name;/ /美国/ Los_Angeles
或者,这个库: https://github.com/Canop/tzdetect.js
其他回答
要检测像“America/New York.”这样的内容,可以使用Luxon库中的新LocalZone()。
import { LocalZone } from 'luxon';
const zoneName = new LocalZone().name;
console.log(新日期()。toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long'}).substring(4).match(/\b(\w)/g).join("))
你可以使用下面的映射表来编写自己的代码: http://www.timeanddate.com/time/zones/
或者,使用moment-timezone库: http://momentjs.com/timezone/docs/
看到zone.name;/ /美国/ Los_Angeles
或者,这个库: https://github.com/Canop/tzdetect.js
如果你已经在使用Moment.js,你可以猜出时区名称:
moment.tz.guess(); // eg. "America/New York"
这得到时区代码(例如,GMT)在旧的javascript(我使用谷歌应用程序脚本与旧引擎):
function getTimezoneName() {
return new Date().toString().get(/\((.+)\)/);
}
我把这个放在这里,以防有人需要。