question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dates and Mongoose

See original GitHub issue

I 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:closed
  • Created 12 years ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
vkarpov15commented, Mar 17, 2016

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.

1reaction
vkarpov15commented, Apr 26, 2016

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found