Node.js and MongoDB Time Zone Issue UTC not being

2019-06-01 06:54发布

问题:

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.