Dates and Mongoose
See original GitHub issueI am running into an interesting issue. I initially started diving into it here:
https://github.com/andrewplummer/Sugar/issues/78
But found out the vanilla JS produces the same issue. Whenever I store a date (no timestamp) and then I go to retrieve that date and format it, its a day behind. There is a simple example in the case, I can get you more info if you need. I’m central time. Here is a snippet of a stored doc in MongoDB that will be a date behind when I format it (e.g. getDate()).
{ "_id" : ObjectId("4eeb6c1822501a661600003b"),
"dateExample" : ISODate("2011-12-19T00:00:00Z")
}
Does mongoose store UTC dates to MongoDB or is there some other reason why this would be?
Issue Analytics
- State:
- Created 12 years ago
- Comments:13
Top Results From Across the Web
Mongoose Tutorials: Working With Dates
Date casting has a couple small cases where it differs from JavaScript's native date parsing. First, Mongoose looks for a valueOf() function on...
Read more >Dates in mongoose? - Stack Overflow
Mongoose converts numeric strings that contain numbers outside the range of representable dates in JavaScript and converts them to numbers before passing ...
Read more >What is Mongoose.prototype.Date in Mongoose? - Educative.io
Mongoose.prototype.Date , or simply Mongoose Date , is a Date SchemaType . ... In the example above, String , Number , and Date...
Read more >How to use mongoose to group by date - ObjectRocket
One of the uses of dates is to group records. In mongoose, we can use the $group stage of aggregation.
Read more >Issue with querying dates with $gte and $lt - MongoDB
I want to make searchDate API (I am using Express, Mongoose and Angular). I need to return list of data between two dates....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No way to do that right now. In the past I’ve just stored dates as a string in ‘YYYYMMDD’ format (like ‘20160317’). This has the nice feature that you can still sort by date and use ordering operators like $gt.
@nasr18
MyModel.find({ timestamp: { $gte: moment().subtract(7, 'days').toDate(), $lte: moment().toDate() } });
Or use https://www.npmjs.com/package/mongodb-moment to make the
toDate()
call unnecessary