How to get the local timezone from the system using nodejs

末鹿安然 提交于 2021-02-06 14:49:27

问题


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

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


回答1:


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.

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.

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

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

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.

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



回答2:


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.




回答3:


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

setup: npm install luxon

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



回答4:


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 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 tip. On almost all servers, mysql also needs to know the tz info. Basically the solution is sudo mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql .. google more about it.)




回答5:


I solved this using 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')

which gives the output :

+05:30



来源:https://stackoverflow.com/questions/38915418/how-to-get-the-local-timezone-from-the-system-using-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!