27

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 ?

Oleg Dats
  • 3,933
  • 9
  • 38
  • 61

5 Answers5

36

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)
Laurent Couvidou
  • 32,354
  • 3
  • 30
  • 46
  • 5
    Interesting solution but it does change the current process timezone. Not acceptable for me. – Stefan Feb 22 '13 at 07:37
  • 8
    @Stefan, set the `process.env.TZ` to some value such as `America/New_York` or `America/Chicago` and watch the timezones change. – weisjohn Sep 13 '13 at 02:42
  • 3
    node-time is not a good option if you deploy on Azure, as we do. You will have problems compiling the bridge ... A better option is timezone-js as that is pure js. It also performs a lot better. – oligofren Jan 06 '14 at 11:43
  • @oligofren Then shouldn't you post this as an answer? – Laurent Couvidou Jan 06 '14 at 17:01
  • eh, Eldar did that in his updated answer. I just commented on this since I went down this route and lost a lot of time, since I was oblivious to the whole cross-compiling madness that awaited when I used node-time :-D – oligofren Jan 06 '14 at 22:42
  • 5
    @weisjohn, setting process.env.TZ from within a running node process will give dangerously wrong results, because libc will already have initialized. Instead it needs to be set before the process starts. – Ed4 Jan 16 '14 at 15:06
  • Node-time doesn't work on Node 15. Moment-timezone solution is better I believe – Gleb Feb 22 '21 at 18:03
16

Moment.js now has Moment Timezone

Install:

npm install --save moment-timezone

Use:

var Moment = require('moment-timezone');
Moment().tz('America/Los_Angeles').format();
pixelfreak
  • 17,714
  • 12
  • 90
  • 109
6

UPDATE: there is another one now:) https://github.com/mde/timezone-js

A timezone-enabled, drop-in replacement for the stock JavaScript Date. The timezoneJS.Date object is API-compatible with JS Date, with the same getter and setter methods -- it should work fine in any code that works with normal JavaScript Dates.


no there is not

But you can use moment.js to make it easier http://momentjs.com/docs/

You still need to know each offset so you will need mapping like {"Europe/Amsterdam":2,"Europe/Kiev":3}

Eldar Djafarov
  • 23,327
  • 2
  • 33
  • 27
2

See Timezone package in npm. It has everything needed built in and is pure JS and seems to be the best timezone handling library available.

https://www.npmjs.com/package/timezone

http://bigeasy.github.io/timezone/

var tz = require('timezone/loaded'),
    equal = require('assert').equal,
    utc;

// Get POSIX time in UTC. 
utc = tz('2012-01-01');

// Convert UTC time to local time in a localize language. 
equal(tz(utc, '%c', 'fr_FR', 'America/Montreal'),
      'sam. 31 déc. 2011 19:00:00 EST');
  • Timezone is a MicroJS library in pure JavaScript with no dependencies that provides timezone aware date math and date formatting.
  • Timezone uses the IANA Database to determine the correct wall clock time anywhere in the world for any time since the dawn of standardized time.
  • Timezone formats dates with a full implementation of strftime formats, including the GNU date extensions.
  • Timezone represents time in POSIX time and local time using RFC 3999 date strings.
  • Timezone is a full featured standards based time library in pure JavaScript for under 3K minified and gzipped.
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
0

Use moment-timezone.js it's a best option to parse, validate & change times over different timezones.

Sudhakar Reddy
  • 338
  • 5
  • 14