Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the local timezone from the system using nodejs

Tags:

node.js

time

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.

like image 768
Malith Avatar asked Aug 12 '16 10:08

Malith


People also ask

How do I get current timezone in node JS?

you can also use moment(). format('Z') to get the same result. This gives you the current offset in your current timezone but this may vary for part of the year due to DST changes. If in doubt, always use/store UTC date/times plus the timezone identifier and then convert to local as late as possible.

How do I find my local time zone?

To get the current browser's time zone, you can use the getTimezoneOffset() method from the JavaScript Date object. The getTimezoneOffset() returns the time difference, in minutes, between UTC time and local time.

How does node js handle timezone?

thejh is right, you cannot change the timezone. Use a JS time library (like moment. js) and add / subtract hours instead. The easiest and the correct way to do this is to simply change your system's time zone.

How do I fetch the timezone of a user?

getTimezoneOffset()/60; The method getTimezoneOffset() will subtract your time from GMT and return the number of minutes. So if you live in GMT-8, it will return 480. To put this into hours, divide by 60.


2 Answers

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 great 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 string for where this event occurred

In modern browsers or node.js, 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" }); 
like image 50
Tim Avatar answered Oct 11 '22 02:10

Tim


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.

like image 37
vkstack Avatar answered Oct 11 '22 02:10

vkstack