How to get local timezone from system using nodejs

Is there a way to get the local time zone from the system (for example: - ubuntu) using nodejs?

I used moment.js to retrieve the date and time values. But could not find a way to extract the time zone.

+9
source share
3 answers

Existing answers will tell you the current time zone offset, but you will have problems if you compare historical / future time points, as this will not take into account daylight saving time changes.

In many time zones, the offset changes throughout the year, and these changes occur on different dates or not at all, depending on latitude. If you only have UTC time and offset, you can never be sure what offset will be in this place at other times of the year.

For example, a UTC + 2: 00 offset may apply to Barcelona in the summer or CΓ΄te d'Ivoire year-round. A 2-hour shift will always display the correct time in CΓ΄te d'Ivoire, but in Barcelona it will be 1 hour within six months.

Check out this article covering above.

How do we handle all these time zone issues? Well, this is pretty simple:

  1. Save all time in UTC
  2. Save the name of the time zone where it happened

In modern browsers, you can get the IANA local time zone string, for example:

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

You can then use this time zone line in a library such as Luxon to help offset your captured UTC time.

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

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 .

+4
source

I decided to use this 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 result:

+05: 30

-2
source

All Articles