node.js 如何使用nodejs从系统中获取本地时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38915418/
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-09-02 20:29:30  来源:igfitidea点击:

How to get the local timezone from the system using nodejs

node.jstime

提问by Malith

Is there a way to obtain the local timezone from the system (eg:- ubuntu) using nodejs?

有没有办法使用 nodejs 从系统(例如:- ubuntu)获取本地时区?

I used moment.js to extract the date and time values. But couldn't find a way to extract the timezone as well.

我使用 moment.js 来提取日期和时间值。但也找不到提取时区的方法。

回答by Tim

The existing answers will tell you the current timezone offset, but you will have issues if you are comparing historic/future points in time as this will not cater for daylight saving changes.

现有的答案会告诉您当前的时区偏移量,但是如果您比较历史/未来的时间点,您会遇到问题,因为这无法满足夏令时的变化。

In many timezones, the offset varies throughout the year and these changes occur at different dates or not at all depending on the latitude. If you only have UTC time and an offset, you can never be sure what the offset will be in that location at various other times during the year.

在许多时区,偏移量全年变化,这些变化发生在不同的日期或根本不发生,具体取决于纬度。如果您只有 UTC 时间和偏移量,则永远无法确定一年中其他时间该位置的偏移量是多少。

For example, a UTC+2:00 offset could refer to Barcelona in the summer or Ivory Coast all year round. The 2hr offset will always display the correct time in Ivory Coast but will be 1hr out for half the year in Barcelona.

例如,UTC+2:00 偏移可以指夏季的巴塞罗那或全年的象牙海岸。2 小时偏移将始终显示在象牙海岸的正确时间,但在巴塞罗那的半年中将显示 1 小时。

Check out this article covering the above.

查看这篇文章,涵盖了上述内容。

How do we cater for all these time zone issues? Well, it's pretty simple:

我们如何解决所有这些时区问题?嗯,这很简单:

  1. Save all times in UTC
  2. Store the time zone name where this occurred
  1. 以UTC格式保存所有时间
  2. 存储发生这种情况的时区名称

In modern browsers, you can get the local IANA time zone string like this:

在现代浏览器中,您可以像这样获取本地 IANA 时区字符串:

Intl.DateTimeFormat().resolvedOptions().timeZone // eg. 'America/Chicago'

You can then use this timezone string in a library like Luxonto help offset your captured UTC times.

然后,您可以在Luxon 等库中使用此时区字符串来帮助抵消您捕获的 UTC 时间。

DateTime.fromISO("2017-05-15T09:10:23", { zone: "Europe/Paris" });

回答by vkstack

It is very simple.

这很简单。

var x = new Date();
var offset= -x.getTimezoneOffset();
console.log((offset>=0?"+":"-")+parseInt(offset/60)+":"+offset%60)

And there is nothing else or you can see if momentJScan help you or not.

没有别的,或者你可以看看momentJS是否可以帮助你。

回答by Tan Nguyen

You can use Luxon refer here: https://moment.github.io/luxon/docs/manual/tour.html

您可以在此处使用 Luxon,请参考:https://moment.github.io/luxon/docs/manual/tour.html

setup: npm install luxon

设置: npm install luxon

var { DateTime } = require('luxon');
let d = DateTime.local();
console.log(d.zoneName); //Asia/Saigon

回答by Malith

I solved this using moment.js (http://momentjs.com/docs/)

我使用 moment.js ( http://momentjs.com/docs/)解决了这个问题

var moment = require('moment');
var offset = moment().utcOffset();
console.log(''.concat(offset < 0 ? "-" : "+",moment(''.concat(Math.abs(offset/60),Math.abs(offset%60) < 10 ? "0" : "",Math.abs(offset%60)),"hmm").format("HH:mm")));

------Edited--------

------编辑------

I found a better solution using moment.js. Just use moment().format('Z')

我找到了一个更好的解决方案moment.js。只需使用moment().format('Z')

which gives the output :

这给出了输出:

+05:30

+05:30