Mongodb 时间戳中的节点申请和插入,日期类型和语言环境时间问题

Node application and insertion in Mongodb timestamp, problem with Date type and locale time

我有一个从 MQTT 接收数据的节点应用程序,其中各种参数和时间戳被发送到代理。服务器和硬件已正确配置本地时间(西班牙)。

当我要在数据库中插入时,在本例中 MongoDB,我格式化时间戳以便能够以 mongo 日期格式插入它。

问题是只有 javascript Date() 可以在 mongo 中插入日期格式,我总是比当地时间少两个小时。

//Format timestamp to date with dayjs
let date = dayjs(data.channels.timestamp * 1000).format()
//Output: 2021-09-16T13:32:33+02:00
//Format date to insert in MongoDB
  let timestamp_ = new Date(date);
//Output: 2021-09-16T11:32:33.000Z 

正确的日期是:2021-09-16T13:32:33+02:00

我试过momentjs,我也遇到了同样的问题。

日期在 GMT+0 时区是正常的。日期对象假定日期始终为 GMT+0。保持这种状态并将所有内容存储在 GMT+0 中。 根据时区而不是存储的数据处理您的查询。