如何在Mongo DB中将日期转换为UTC

时间:2020-10-27 21:47:40

标签: node.js mongodb mongoose

我正在尝试将开始日期字段与当前日期进行比较,但是“ new Date()”将转换为本地时间。有人可以帮我将新的date()转换为UTC日期格式,因为startDate字段与UTC日期一起存储在数据库中。


database.Models.EventModel.findOne({
    $and: [
      { 'status': 'Accepted' },
      { 'startDate': { $gte: new Date() } }
 ],
  }).sort('-startDate');

1 个答案:

答案 0 :(得分:1)

按照:When MongoDB inserts a date, it is converting it to UTC

默认情况下,MongoDB将时间存储在UTC中,并将转换任何本地时间 时间表示成这种形式。必须运行的应用程序 报告一些未修改的本地时间值可能会存储时区 与UTC时间戳一起,并计算原始本地时间 他们的应用逻辑。

https://docs.mongodb.com/v3.2/tutorial/model-time-data/

new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.
new Date("<YYYY-mm-ddTHH:MM:ss>") specifies the datetime in the client’s local timezone and returns the ISODate with the specified datetime in UTC.
new Date("<YYYY-mm-ddTHH:MM:ssZ>") specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC.
new Date(<integer>) specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance.
相关问题