Javascript 如何在 Nodejs 中使用时区偏移?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10615828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:09:57  来源:igfitidea点击:

How to use timezone offset in Nodejs?

javascriptnode.jsdatetimetimezone

提问by Oleg Dats

I need the next flow:

我需要下一个流程:

var a = new Date(1337324400000, 'Europe/Amsterdam'); //+2h
console.log(a); // for example 12:00 Mon ...
a.setTimeZone('Europe/Kiev'); //+3h
console.log(a); // 13:00 Mon ...

Is there such possibility in nodejs utils api ?

nodejs utils api 中是否有这种可能性?

回答by Laurent Couvidou

You can use node-time, as follows:

您可以使用node-time,如下所示:

var time = require('time');

var a = new time.Date(1337324400000);

a.setTimezone('Europe/Amsterdam');
console.log(a.toString()); // Fri May 18 2012 09:00:00 GMT+0200 (CEST)
a.setTimezone('Europe/Kiev');
console.log(a.toString()); // Fri May 18 2012 10:00:00 GMT+0300 (EEST)

回答by pixelfreak

Moment.js now has Moment Timezone

Moment.js 现在有Moment 时区

Install:

安装

npm install --save moment-timezone

Use:

使用

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

回答by Eldar Djafarov

UPDATE: there is another one now:) https://github.com/mde/timezone-js

更新:现在还有一个:) https://github.com/mde/timezone-js

A timezone-enabled, drop-in replacement for the stock JavaScript Date. The timezoneJS.Date object is API-compatible with JS Date, with the same getter and setter methods -- it should work fine in any code that works with normal JavaScript Dates.

支持时区的直接替代股票 JavaScript 日期。timezoneJS.Date 对象与 JS Date API 兼容,具有相同的 getter 和 setter 方法——它应该可以在任何使用普通 JavaScript 日期的代码中正常工作。



no there is not

不,那里没有

But you can use moment.jsto make it easier http://momentjs.com/docs/

但是您可以使用moment.js使其更容易http://momentjs.com/docs/

You still need to know each offset so you will need mapping like {"Europe/Amsterdam":2,"Europe/Kiev":3}

您仍然需要知道每个偏移量,因此您需要映射 {"Europe/Amsterdam":2,"Europe/Kiev":3}

回答by Samuel Neff

See Timezone package in npm. It has everything needed built in and is pure JS and seems to be the best timezone handling library available.

请参阅 npm 中的时区包。它内置了所需的一切,并且是纯 JS,似乎是可用的最佳时区处理库。

https://www.npmjs.com/package/timezone

https://www.npmjs.com/package/timezone

http://bigeasy.github.io/timezone/

http://bigeasy.github.io/timezone/

var tz = require('timezone/loaded'),
    equal = require('assert').equal,
    utc;

// Get POSIX time in UTC. 
utc = tz('2012-01-01');

// Convert UTC time to local time in a localize language. 
equal(tz(utc, '%c', 'fr_FR', 'America/Montreal'),
      'sam. 31 déc. 2011 19:00:00 EST');
  • Timezone is a MicroJS library in pure JavaScript with no dependencies that provides timezone aware date math and date formatting.
  • Timezone uses the IANA Database to determine the correct wall clock time anywhere in the world for any time since the dawn of standardized time.
  • Timezone formats dates with a full implementation of strftime formats, including the GNU date extensions.
  • Timezone represents time in POSIX time and local time using RFC 3999 date strings.
  • Timezone is a full featured standards based time library in pure JavaScript for under 3K minified and gzipped.
  • Timezone 是一个纯 JavaScript 的 MicroJS 库,没有提供时区感知日期数学和日期格式的依赖项。
  • 时区使用 IANA 数据库来确定自标准化时间出现以来世界任何地方的正确挂钟时间。
  • 时区使用 strftime 格式的完整实现来格式化日期,包括 GNU 日期扩展。
  • 时区使用 RFC 3999 日期字符串以 POSIX 时间和本地时间表示时间。
  • Timezone 是一个全功能的基于标准的纯 JavaScript 时间库,用于 3K 以下的缩小和 gzip。

回答by Sudhakar Reddy

Use moment-timezone.jsit's a best option to parse, validate & change times over different timezones.

使用moment-timezone.js是解析、验证和更改不同时区时间的最佳选择。