Node: I can't set a default timezone on windows.

Created on 10 Dec 2015  路  17Comments  路  Source: nodejs/node

This code works well on Linux but does not work on windows.

process.env.TZ = 'UTC';
console.log(new Date());

On windows i see timezone from my os.

Thu Dec 10 2015 14:28:45 GMT+0300

V8 Engine question windows

Most helpful comment

This problem _still_ exists two years later.

All 17 comments

Does it work when you set TZ on the command line? V8 calls _tzset() on Windows, which respects TZ, but it runs quite early on.

The same caveat applies to Unices; the fact that setting TZ inside the program works is luck more than anything else. If you want it to be reliable and future proof, run your program with env TZ=UTC node app.js.

@bnoordhuis Yes, I tried to set an environment variable, set "TZ=UTC" is not work.

What version of Windows is that? Do you get correct output after tzutil /s UTC?

Windows 10x64

Thu Dec 10 2015 12:42:57 GMT+0000

It works, but it changes the time zone of the OS.

It works, but it changes the time zone of the OS.

Right, but it confirms that the timezone calculation as such is correct once it has the right timezone.

/cc @nodejs/platform-windows - I'm fairly sure it's a V8 issue (relevant code here) but maybe you can take a look?

Is it even supposed to work the way it does on Linux? Is it documented anywhere?

V8 documentation? Surely you jest! If you're referring to _tzset(), MSDN claims it honors TZ.

No, I'm referring to how the TZ variable affects the behavior of new Date() in Node.js.

Correction: new Date() is not affected, the implicit toString() call is.

Neither we nor V8 document the effect of TZ, it's an artifact of the implementation.

If you're referring to _tzset(), MSDN claims it honors TZ.

GetTimeZoneInformation, however, does not - and that's where V8 gets timezone information from. I'm not sure if there is any point to the _tzset() call at all.

Closing as there does not appear to be anything for us to do in core on this. Can reopen if necessary.

Hello,

I've been struggling with same problem.
Under windows, TZ setting is not respected, zone is taken from user which runs app.
Under Linux it works as expected.

I'm running production enviroment where I can't change zone for user - what should I do?

Thanks!

@pankleks the only solution I've come up for windows is: https://github.com/capaj/set-tz

This problem _still_ exists two years later.

I've created a ugly batch script for this issue.

package.json

"scripts": {
  "test": "jest",
  "test:win": "bin/test.cmd"
}

bin/test.cmd

FOR /F "tokens=* USEBACKQ" %%F IN (`tzutil /g`) DO SET PREVIOUS_TZ=%%F
tzutil /s "UTC"
cmd.exe /c yarn test
tzutil /s "%PREVIOUS_TZ%"

This problem still exists 4 years later

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cong88 picture cong88  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

willnwhite picture willnwhite  路  3Comments