3

I am attempting to work with dates in my project. I keep getting weird little changes based on the dates being 5 hours off (I live in EST, -5).

The production environment defaults to UTC and I would love to be able to do that as well in my localhost environment.

Is there a way that I may use express or some npm package to start the server off in UTC? Is this a machine issue?

I do not want to have different code than the production environment.

Phil
  • 10,948
  • 17
  • 69
  • 101
  • A robust application should not break when it's *not* running in UTC. Better to just fix your bugs than to force the environment to take on the UTC timezone. – Evert Nov 08 '20 at 04:37
  • @Evert Umm, new Date() gives multiple different results. I would like to make them both hit UTC even if nodejs is local. Unless you mean by "robust system" I convert it to UTC either way? No thanks. – Phil Nov 09 '20 at 15:48
  • Yes, they are different. However, you can still get UTC time from a Date object in local time. – Evert Nov 09 '20 at 16:20
  • Awesome! Now if you would explain to me how to do that with the same code base, that would amazing and answer this question! – Phil Nov 09 '20 at 18:16
  • Well, have you seen: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC – Evert Nov 09 '20 at 18:53

5 Answers5

5

Another answer would be to set the env variables.

env TZ='Europe/Amsterdam' node server.js

Answer credits goes to https://stackoverflow.com/a/35432069/1281089

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
4

The easiest way would be to run the code in a Docker container. You can set the time of the container to UTC.

Dockerizing a NodeJS based app is very easy.

A sample Docker file.

FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080

# Actual timezone env variable set
ENV TZ Europe/Amsterdam

CMD [ "npm", "start" ]

Most of the time you don't even need to manually set the time to UTC. The default time will be UTC in the docker container.

Internet is filled with tutorials on how to deploy a nodeJS app to docker. A good one is https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
  • 1
    Hmm, I was hoping for a one liner... though docker seems easy I don't know how to do it just yet – Phil Jan 07 '18 at 04:04
  • What if my docker containers are located in europe but the software is supporting people in asia? Would I have to alter my dockers correct timezone to configure node? – JoshuaTree Oct 27 '18 at 09:29
  • 1
    This doesn't show how to set container's time to UTC nor how to change its timezone. – Vinas Nov 06 '20 at 20:12
3

I would have suggested Moment.js as well but if you look at something more global, 2 hints :

  1. Compare the version of node between the prod and your local env :

https://github.com/nodejs/node-v0.x-archive/issues/3044

=> You may have some version differences explaining the different behaviours

  1. Force UTC on your local env :

    export TZ=UTC (on linux)

    $env:TC = 'UTC' (on windows)

=> source : Can I make node.js Date always be in UTC/GMT?

Steve Lng C
  • 1,204
  • 1
  • 8
  • 9
-2

I highly suggest checking out Moment.js for handling dates and times in JS.

You should handle dates and times in your code as follows.

moment.utc(myDate).format();

See docs here.

Your code will then use dates and times in UTC regardless of where it's running. This way, your code will show the same dates and times locally as in production.

Dharman
  • 30,962
  • 25
  • 85
  • 135
-2

STEP 1: Change the Windows OS time to UTC and United Kingdom Region (for military time).

STEP 2: In Chrome dev tools, set your sensor to the timezone that you wish. In my case it was EDT/EST

Notes:

Make sure you have dev tools open when you want to implement the browser timezone change. Also, you might have to toggle the sensor back and forth on new tab to make it work again.

Here is to hoping NodeJS for windows figures something out.

Phil
  • 10,948
  • 17
  • 69
  • 101