Skip to content

I can't set a default timezone on windows. #4230

Description

@Claud

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

Activity

  1. added
    questionIssues asking questions about Node.js.
    windowsIssues and PRs related to the Windows platform.
    on Dec 10, 2015
  2. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

    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.

  3. Claud commented on Dec 10, 2015

    @Claud
    Author

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

  4. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

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

  5. Claud commented on Dec 10, 2015

    @Claud
    Author

    Windows 10x64

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

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

  6. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

    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?

  7. seishun commented on Dec 10, 2015

    @seishun
    Contributor

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

  8. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

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

  9. seishun commented on Dec 10, 2015

    @seishun
    Contributor

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

  10. seishun commented on Dec 10, 2015

    @seishun
    Contributor

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

  11. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

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

  12. added
    v8 engineIssues and PRs related to the V8 dependency.
    on Dec 10, 2015
  13. seishun commented on Dec 10, 2015

    @seishun
    Contributor

    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.

  14. jasnell commented on Jun 6, 2016

    @jasnell
    Member

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

  15. 47 remaining items

  16. WiXSL commented on Oct 31, 2021

    @WiXSL

    I can confirm this problem is solved in node version 17.0.1 using Windows 10

  17. pavelmickevic commented on Dec 9, 2021

    @pavelmickevic

    thank you for the fix 16cb4f7

    I can confirm this problem is solved in node version 17.0.1 using Windows 10

    works now:

    node -v
    v17.2.0

    OS Name: Microsoft Windows 10 Pro
    OS Version: 10.0.19041 N/A Build 19041

    -console.log({ tz: process.env.TZ, offset: moment().utcOffset() }); // { tz: 'UTC', offset: 120 } # bad
    +console.log({ tz: process.env.TZ, offset: moment().utcOffset() }); // { tz: 'UTC', offset: -0 } # good
  18. tmeindle commented on Feb 15, 2022

    @tmeindle

    Could the fix for this issue be backported to Node 16 LTS?

  19. richardlau commented on Feb 15, 2022

    @richardlau
    Member

    Could the fix for this issue be backported to Node 16 LTS?

    #38642 was included in Node.js 16.2.0.

  20. phalgunv commented on Apr 30, 2023

    @phalgunv

    Is this fix available on Node.js v14?

  21. richardlau commented on May 15, 2023

    @richardlau
    Member

    Is this fix available on Node.js v14?

    No, it was not backported to Node.js v14.

  22. phalgunv commented on May 15, 2023

    @phalgunv

    What can be done to get it ported back to v14?

  23. targos commented on May 15, 2023

    @targos
    Member

    Nothing. v14 is out of support now.

  24. NisuSan commented on May 29, 2023

    @NisuSan

    Not working if send the timesatamp to contructor

    $ node -pe "new Date(1685389932472)"
    2023-05-29T19:52:12.472Z
    
    $ node -pe "new Date()"
    2023-05-29T20:21:51.841Z 
    
    $ set TZ="Europe/Kiev" && node -pe "new Date(1685389932472)" #still not working
    2023-05-29T19:52:12.472Z

    But timesatmp is Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)

  25. mattjohnsonpint commented on May 31, 2023

    @mattjohnsonpint

    @NisuSan - Time zones don't change the UTC representation of a timestamp.

  26. NisuSan commented on Jun 12, 2023

    @NisuSan

    @mattjohnsonpint Hmm, in browser it works fine. Any way, can you explain how to deal with timestamp and time zone in this case?

  27. mattjohnsonpint commented on Jun 12, 2023

    @mattjohnsonpint

    @NisuSan - The Z at the end of the ISO 8601 / RFC 3339 formatted string represents "Zulu" time - which is another term for "UTC". Unix timestamps are also in terms of UTC (being the number of seconds or milliseconds since 1970-01-01T00:00:00.000Z). UTC is irrespective of time zones, thus time zones do not affect UTC output.

    If you're asking why you see UTC output instead of local output, that's because you're just printing a raw Date object. You didn't call any function on it that creates a string. Unfortunately, the implementation of printing a raw Date object is undefined. Some environments print the UTC representation (as if you printed the result of calling the toISOString function), and other environments print the local time representation (as if you printed the result of calling the toString function).

    I have asked for standardization of this, to avoid the type of confusion you encountered, but with no result. See whatwg/console#132

  28. mattjohnsonpint commented on Jun 12, 2023

    @mattjohnsonpint

    If you want to see the effect of the system time zone, and Node's support for changing it, then you'll need to call toString - not just print the raw Date object.

    on Windows:

    set TZ=Europe/Kiev && node -pe "new Date(1685389932472).toString()"
    Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)
    

    Note: Make sure there are no quotes around the ID on Windows.

    or on Linux / macOS :

    TZ="Europe/Kiev" node -pe "new Date(1685389932472).toString()"
    Mon May 29 2023 22:52:12 GMT+0300 (Eastern European Summer Time)
    
  29. NisuSan commented on Jun 12, 2023

    @NisuSan

    @mattjohnsonpint Great explanation, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    i18n-apiIssues and PRs related to Node.js internationalization support.questionIssues asking questions about Node.js.v8 engineIssues and PRs related to the V8 dependency.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions