Node.js和MongoDB时区问题驱动程序未正确转换UTC? [英] Node.js and MongoDB Time Zone Issue UTC not being converted correctly by driver?

查看:131
本文介绍了Node.js和MongoDB时区问题驱动程序未正确转换UTC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发生了一件奇怪的事情,希望有人能指出我所缺少的东西.

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

在MongoDB中,我有一个日期类型为DT的字段

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

MongoDB中日期显示的示例为2014-10-01 10:28:04.329-04:00

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

当我使用MongoClient从Node.js查询MongoDB时,Node.js返回此信息:

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

2014-10-01T14:28:04.329Z

据我了解,驱动程序应该将UTC转换为本地时间.就我而言,应该是美国东部时间(EDT).为什么Node要增加4个小时呢?

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?

我正在使用Java驱动程序从Java将日期加载到MongoDB中.变量是使用

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

new Date();

推荐答案

节点没有增加4个小时.两者都显示完全相同的瞬间.

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

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

2014-10-01T14:28:04.329Z

只有一个在EDT时区中,该时区与UTC的时间相距-04:00(因此比该时间早了四个小时),另一个在UTC中.

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.

可能您已在EDT中配置了服务器,并且客户端已设置为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.

或更妙的是,将客户端计算机和服务器计算机设置为相同的时区,最好是UTC .

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

这篇关于Node.js和MongoDB时区问题驱动程序未正确转换UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆