Malith Malith - 3 months ago 21
Node.js Question

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?

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

Answer

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

Comments