简体   繁体   English

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

[英]How to get the local timezone from the system using nodejs

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.我使用 moment.js 提取日期和时间值。 But couldn't find a way to extract the timezone as well.但也找不到提取时区的方法。

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.例如,UTC+2:00 偏移可以指夏季的巴塞罗那或全年的象牙海岸。 The 2hr offset will always display the correct time in Ivory Coast but will be 1hr out for half the year in Barcelona. 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以UTC格式保存所有时间
  2. Store the time zone name where this occurred存储发生这种情况的时区名称

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 Luxon to help offset your captured UTC times.然后,您可以在Luxon 等库中使用此时区字符串来帮助抵消您捕获的 UTC 时间。

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

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 momentJS can help you or not.没有别的,或者你可以看看momentJS是否可以帮助你。

It is this easy, no libraries needed:就这么简单,不需要库:

console.log("test ...")
let d = new Date()
console.log("UTC time " + d)
let ank = d.toLocaleString('en-US', { timeZone: 'America/Anchorage' });
console.log("your time zone " + ank)

在此处输入图片说明

How to see the exact time zone names on most servers:如何在大多数服务器上查看确切的时区名称:

ls /usr/share/zoneinfo

Works flawlessly:完美运行:

You'll get the correct time-text regardless of daylight savings issues, etc etc.无论夏令时问题等如何,您都会获得正确的时间文本。


Handy related mysql tip:方便的相关 mysql 提示:

On almost all servers, mysql also needs to know the tz info.在几乎所有服务器上,mysql 也需要知道 tz 信息。

Basically the solution is, on the shell基本上解决方案是,在外壳上

sudo mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql

.. google more about it. .. 谷歌更多关于它。

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

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 .我使用moment.js找到了更好的解决方案。 Just use moment().format('Z')只需使用moment().format('Z')

which gives the output :这给出了输出:

+05:30 +05:30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM