Node.js and MongoDB Time Zone Issue UTC not being converted correctly by driver?

ぐ巨炮叔叔 提交于 2020-01-02 23:05:20

问题


I have a strange thing occurring and I hope someone can point out what i am missing.

In MongoDB I have a field DT that is of Type Date

An example of what the date looks like in MongoDB is 2014-10-01 10:28:04.329-04:00

When I query MongoDB from Node.js using MongoClient, Node.js is returning this:

2014-10-01T14:28:04.329Z

As i understand it the driver is suppose to convert UTC to local time. In my case it should be Eastern Time (EDT). Why would Node be adding 4 hours instead?

I am loading the date into MongoDB from Java using the Java driver. The variable is set using

new Date();

回答1:


Node isn't adding 4 hours. Both show exactly the same instant.

2014-10-01 10:28:04.329-04:00

is exactly the same as

2014-10-01T14:28:04.329Z

only one is in a EDT timezone which has -04:00 offset to UTC (so it's four hours earlier there), and the other is in UTC.

Probably you have your server configured in EDT and your client is set to UTC or the other way around.

Unless you need the exact same strings, I wouldn't worry about it.

Or, even better, set both the client and server machine to the same timezone, preferably UTC.



来源:https://stackoverflow.com/questions/26150495/node-js-and-mongodb-time-zone-issue-utc-not-being-converted-correctly-by-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!