Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1355 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the local timezone from the system using nodejs

#1
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.
Reply

#2
I solved this using moment.js (

[To see links please register here]

)



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


Reply

#3
## 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)

[![enter image description here][1]][1]


## 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:

On almost all servers, mysql also needs to know the tz info.

Basically the solution is, on the shell

`sudo mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql`

.. google more about it.

[1]:
Reply

#4
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.](

[To see links please register here]

)

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 see links please register here]

) to help offset your captured UTC times.

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

#5
It is very simple.

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

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

Note: This answer is outdated, you can suggest to add in it.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through