【发布时间】:2018-03-19 10:36:14
【问题描述】:
我正在尝试获取 startDate 和 endDate 之间“日期”字段的所有记录。
我正在用这个查询数据库(获取从月初到今天的记录):
var startDate = new Date(2017,09,1);
var endDate = new Date(2017,09,7);
//console.log(startDate) = Sun Oct 01 2017 00:00:00 GMT-0300 (BRT)
//console.log(endDate) = Sat Oct 07 2017 00:00:00 GMT-0300 (BRT)
Sale.find({date:{"$gte":startDate,"$lt":endDate}},function(err, sales){
}
日期在 10/02 和 10/07 之间的记录将被退回,但日期为 10/01 的记录不会被退回。以下是这些缺失记录的日期字段:
"date": {
"$date": "2017-10-01T23:00:00.000Z"
}
我将小时设置为 23 以查看是否会有所不同(时区等),但没有。
如果我将 startDate 设置为 9 月 30 日 23 时,什么都没有。 22、21 小时,什么都没有。 当 startDate 变为 9 月 30 日 20 小时宾果游戏时,返回记录。
这可能是一个时区问题,但我无法确定在哪里以及如何。
【问题讨论】:
-
在 JS 中,
new Date(2017,09,1);返回Wed Nov 01 2017 00:00:00 GMT-0400 (EDT),因为 0 是一月。但是,这并不能解释您所看到的行为。 -
您的数据在哪个时区写入 MongoDB? UTC 还是本地?
-
本地的,但是新的 Date() 对象也应该是本地的吧?我用日期的 console.log 更新了问题。
-
Sun Oct 01 2017 23:00:00 GMT-0300 (BRT)将是Sun Oct 02 2017 02:00:00 GMT-0000,因此将其从 2300 移至 1600 并看看会发生什么。 -
但是 Oct 02 大于 startDate,所以应该返回!
标签: javascript node.js mongodb date