Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Created August 1, 2025 18:56
Show Gist options
  • Save ryuheechul/0db1f9ea82ea3900738addc1ea45f286 to your computer and use it in GitHub Desktop.
Save ryuheechul/0db1f9ea82ea3900738addc1ea45f286 to your computer and use it in GitHub Desktop.
(default) timezone (with Node.js)

Well, first of all, there is https://www.stefanjudis.com/today-i-learned/set-the-default-time-zone-in-node-js/.

The motivation here for me is to set timezone for Node.js (in a container).

This is a Containerfile that I came up with to show few different way to set TZ env var which is a sufficient way to setup timezone for a Node.js process.

FROM registry.access.redhat.com/ubi9/nodejs-22-minimal

RUN echo 'without specifying TZ'
RUN node -e "console.log(new Date().toString())"
# will be whatever the default timezone (UTC unless further configured on runtime)

RUN echo 'with specifying TZ for node command only'
RUN TZ=Asia/Seoul node -e "console.log(new Date().toString())"
# will be Asia/Seoul timezone

RUN echo 'with exporting TZ'
RUN export TZ=America/New_York; node -e "console.log(new Date().toString())"
# will be America/New_York timezone

RUN echo 'within the node, before and after setting TZ'
RUN node -e "console.log(new Date().toString()); process.env.TZ='Europe/Bucharest'; console.log(new Date().toString())"
# will be the container default timezone first and Europe/Bucharest timezone after

RUN echo 'with specifying TZ for the container'
ENV TZ=America/Vancouver
RUN node -e "console.log(new Date().toString())"
# will be America/Vancouver timezone

You may run with podman build . or podman build . --env=TZ=Europe/Paris (or docker build . -f Containerfile).

And the result:

STEP 1/12: FROM registry.access.redhat.com/ubi9/nodejs-22-minimal
STEP 2/12: RUN echo 'without specifying TZ'
without specifying TZ
--> a5114caf2850
STEP 3/12: RUN node -e "console.log(new Date().toString())"
Fri Aug 01 2025 18:31:12 GMT+0000 (Coordinated Universal Time)
--> 829d027dc534
STEP 4/12: RUN echo 'with specifying TZ for node command only'
with specifying TZ for node command only
--> 4d621143ba11
STEP 5/12: RUN TZ=Asia/Seoul node -e "console.log(new Date().toString())"
Sat Aug 02 2025 03:31:14 GMT+0900 (Korean Standard Time)
--> 189b07d7c824
STEP 6/12: RUN echo 'with exporting TZ'
with exporting TZ
--> d459f2441c72
STEP 7/12: RUN export TZ=America/New_York; node -e "console.log(new Date().toString())"
Fri Aug 01 2025 14:31:16 GMT-0400 (Eastern Daylight Time)
--> 3a197d264c45
STEP 8/12: RUN echo 'within the node, before and after setting TZ'
within the node, before and after setting TZ
--> eeb62610ef55
STEP 9/12: RUN node -e "console.log(new Date().toString()); process.env.TZ='Europe/Bucharest'; console.log(new Date().toString())"
Fri Aug 01 2025 18:31:18 GMT+0000 (Coordinated Universal Time)
Fri Aug 01 2025 21:31:18 GMT+0300 (Eastern European Summer Time)
--> 7eeec2a7baf2
STEP 10/12: RUN echo 'with specifying TZ for the container'
with specifying TZ for the container
--> 27d4c707ef20
STEP 11/12: ENV TZ=America/Vancouver
--> eeb51d149f51
STEP 12/12: RUN node -e "console.log(new Date().toString())"
Fri Aug 01 2025 11:31:20 GMT-0700 (Pacific Daylight Time)
COMMIT
--> 424359791694
424359791694a55293ed75cc7a562292f2c739bf862bbae1a762626662204f6b

And third-party library like momentjs should be affected by TZ, https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/.

Although I used a container here, any Unix or POSIX system without container should work the same with these commands.

Lastly, not super related to this gist but I also made this last year, https://superhandy.tools/time/world/ to help reduce the mental load between timezone related conversions.

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