Docker-node: node:8-alpine wrong timezone behavior

Created on 13 Feb 2018  路  8Comments  路  Source: nodejs/docker-node

Hi all,

I have found a strange behavior in node:8-alpine. Some methods of the Date object return incorrect values.

The getTimezoneOffset() method return 0 instead of -60 for Paris TZ

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().getTimezoneOffset()"
-> 0

Also the getHours method return the non local hour:

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().getHours()"
-> 10

But the toLocaleTimeString method return the correct hour.

docker container run -e TZ="Europe/Paris" -it --rm node:8-alpine node -p "new Date().toLocaleTimeString()"
-> 11:xx:xx

So it seem that the methods getHours don't take into account the TZ env var.

Most helpful comment

This issue is not exactly answered. Having this same issue on node:10-alpine. Tried solution above and whatever I could find in gliderlabs/docker-alpine/issues/136. But to no avail. When doing console.log(new Date()) the issue persists in node.js; time differs minus 2 hours from what busybox says when running the date command.

All 8 comments

You need to run the following

RUN apk --update add \
        tzdata \
    && cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    && apk del tzdata

Info from gliderlabs/docker-alpine/issues/136

Closing as it seems answered? Happy to reopen

This issue is not exactly answered. Having this same issue on node:10-alpine. Tried solution above and whatever I could find in gliderlabs/docker-alpine/issues/136. But to no avail. When doing console.log(new Date()) the issue persists in node.js; time differs minus 2 hours from what busybox says when running the date command.

I had the same issue and found that setting environment variable TZ in the Dockerfile solves it. At least for me it did.

RUN apk --update add \ tzdata \ && cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && apk del tzdata ENV TZ=America/New_York

Having same issue don't know what the problem is. Running date in the shell returns the correct time but running new Date in the node REPL returns current time minus 2hrs which seems to be GMT at the time of this writing. Also can't get nodecron to work properly when setting the timezone there instead, it works against the same offset.
Something is messed up with how node gets the time from the OS.
At the same time the below works for debian stretch with no issue:

RUN ln -fs /usr/share/zoneinfo/Europe/Copenhagen /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata

new Date is returned in UTC https://tc39.es/ecma262/#sec-date-constructor-date

@nschonni
Okay, but the offset is not recognized either:
i.e. (new Date()).getTimezoneOffset() returns 0. It should return -120.

Just ran into this myself on node:12-alpine:

$ date
Fri Aug  7 21:21:25 PDT 2020

$ node
Welcome to Node.js v12.18.3.
Type ".help" for more information.
> process.env.TZ
undefined
> new Date().toLocaleString();
'8/7/2020, 7:22:25 PM'

$ TZ=America/Los_Angeles node
Welcome to Node.js v12.18.3.
Type ".help" for more information.
> process.env.TZ
'America/Los_Angeles'
> new Date().toLocaleString();
'8/7/2020, 9:25:24 PM'

I'm guessing this means node depends on the TZ environment variable being set. Although, the 2 hours off thing is just strange.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bfaulk96 picture bfaulk96  路  4Comments

actualben picture actualben  路  3Comments

mtibben picture mtibben  路  3Comments

frankbaele picture frankbaele  路  3Comments

eyaylagul picture eyaylagul  路  3Comments