简体   繁体   English

在 NodeJS localhost 中设置时区以反映生产环境

[英]Setting timezone in NodeJS localhost to reflect production environment

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).基于 5 小时休息的日期(我住在美国东部时间,-5),我不断收到奇怪的小变化。

The production environment defaults to UTC and I would love to be able to do that as well in my localhost environment.生产环境默认为 UTC,我希望在我的本地主机环境中也能做到这一点。

Is there a way that I may use express or some npm package to start the server off in UTC?有没有办法可以使用express或某些npm包在 UTC 中启动服务器? Is this a machine issue?这是机器问题吗?

I do not want to have different code than the production environment.我不想拥有与生产环境不同的代码。

Another answer would be to set the env variables.另一个答案是设置 env 变量。

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

Answer credits goes to https://stackoverflow.com/a/35432069/1281089回答学分转到https://stackoverflow.com/a/35432069/1281089

I would have suggested Moment.js as well but if you look at something more global, 2 hints :我也会建议 Moment.js 但如果你看看更全局的东西,2 个提示:

  1. Compare the version of node between the prod and your local env :比较 prod 和本地 env 之间的节点版本:

https://github.com/nodejs/node-v0.x-archive/issues/3044 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 :在您的本地环境上强制使用 UTC:

    export TZ=UTC (on linux)导出 TZ=UTC(在 Linux 上)

    $env:TC = 'UTC' (on windows) $env:TC = 'UTC'(在 Windows 上)

=> source : Can I make node.js Date always be in UTC/GMT? => 来源: 我可以让 node.js 日期总是在 UTC/GMT 吗?

The easiest way would be to run the code in a Docker container.最简单的方法是在 Docker 容器中运行代码。 You can set the time of the container to UTC.您可以将容器的时间设置为 UTC。

Dockerizing a NodeJS based app is very easy. Dockerizing 一个基于 NodeJS 的应用程序非常简单。

A sample Docker file.一个示例 Docker 文件。

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.大多数情况下,您甚至不需要手动将时间设置为 UTC。 The default time will be UTC in the docker container. docker 容器中的默认时间将为 UTC。

Internet is filled with tutorials on how to deploy a nodeJS app to docker.互联网上到处都是关于如何将 nodeJS 应用程序部署到 docker 的教程。 A good one is https://nodejs.org/en/docs/guides/nodejs-docker-webapp/一个好的是https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

I highly suggest checking out Moment.js for handling dates and times in JS.我强烈建议查看Moment.js以在 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.然后,您的代码将使用 UTC 格式的日期和时间,而不管它在哪里运行。 This way, your code will show the same dates and times locally as in production.这样,您的代码将在本地显示与生产中相同的日期和时间。

STEP 1: Change the Windows OS time to UTC and United Kingdom Region (for military time). STEP 1:将 Windows 操作系统时间更改为UTCUnited Kingdom Region (军用时间)。

STEP 2: In Chrome dev tools, set your sensor to the timezone that you wish. STEP 2:在 Chrome 开发工具中,将传感器设置为您希望的时区。 In my case it was EDT/EST就我而言,它是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.这是希望 Windows 的NodeJS能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM