use*_*256 4 javascript datetime node.js
我有一个 UTC 日期时间字符串和时区名称。我是从第三方 API 获得的。我需要使用 JavaScript/NodeJS 将 UTC 时间转换为该时区的本地时间,并获取该时区的当前时间。是否有任何可用的库/方法?
var timezone = "America/New_York";//This will always be in Olson format.
var UTCTime = "2017-09-03T02:00:00Z";
var localTime; //I need to find the equivalent of UTCTime for America/New_York
var currentTime; //Current time of time zone America/New_York
Run Code Online (Sandbox Code Playgroud)
这可以通过以下几种方式完成:
var options = {
timeZone: "America/New_York",
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric'
};
var formatter = new Intl.DateTimeFormat([], options);
var UTCTime = "2017-09-03T02:00:00Z";
var localTime = formatter.format(new Date(UTCTime));
var currentTime = formatter.format(new Date()); console.log(currentTime, localTime);
Run Code Online (Sandbox Code Playgroud)
或者你可以使用moment.js库。
您可以在最现代的浏览器中使用 Intl.DateTimeFormat。 大多数现代浏览器都支持Intl.DateTimeFormat。