How to use timezone offset in Nodejs?


Question

I need the next flow:

var a = new Date(1337324400000, 'Europe/Amsterdam'); //+2h
console.log(a); // for example 12:00 Mon ...
a.setTimeZone('Europe/Kiev'); //+3h
console.log(a); // 13:00 Mon ...

Is there such possibility in nodejs utils api ?

1
22
5/16/2012 9:36:32 AM

Accepted Answer

You can use node-time, as follows:

var time = require('time');

var a = new time.Date(1337324400000);

a.setTimezone('Europe/Amsterdam');
console.log(a.toString()); // Fri May 18 2012 09:00:00 GMT+0200 (CEST)
a.setTimezone('Europe/Kiev');
console.log(a.toString()); // Fri May 18 2012 10:00:00 GMT+0300 (EEST)
33
5/30/2012 11:47:28 PM

Moment.js now has Moment Timezone

Install:

npm install --save moment-timezone

Use:

var Moment = require('moment-timezone');
Moment().tz('America/Los_Angeles').format();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon