I'm in Central European Summer Time (CEST) at 15:08 I added document to my collection. In model to new Schema
I'm passing timestamps: true
. Its createdAt is incorrect, it's two hours too early:
createdAt:2019-08-21 13:08:39.219
Why? Also what does .219
at the end mean? Lastly how can I query this collection to get every one which createdAt is within last 30 minutes?
const c = await Collection.find({
createdAt: {
$gte: new Date() // ???
}
});
I've combined answers with comments and tried something like this:
const d = new Date(
new Date().getTime() + new Date().getTimezoneOffset() * 60000 - 30 * 60000
);
const c = await Collection.find({
createdAt: { $gte: d }
});
but it doesn't seem to work correctly. I execute this code at 15:47 CEST which is 13:47 UTC so earliest document should be from 13:17 but I get results with "createdAt": "2019-08-21T12:43:38.395Z",
.
You can create a date that ignores your browser timezone:
function newDateTZSafe(){
var now = new Date();
return new Date(now.getTime() + now.getTimezoneOffset() * 60000);
}
or as alternative, if you heavily rely on dates, use moment.js
**Try this query**
const c = await Collection.find({
createdAt: {
"$gte" : new Date(ISODate().getTime() - 1000 * 60 * 30) }
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.