Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get previous day's records in MongodDB?

I am using MongoDB which has a time field in milliseconds. I am trying to get all the records for previous day.
So far I have searched for solutions which show how to get the records for previous 24 hrs. But I need records for previous day only. Please help to write this query.
So something like this

createdTime: {
     $lt: new Date().getTime(),
     $gt: new Date().getTime() - 1000 * 60 * 60 * 24,
},

But like I explained this is not what I want.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get the UNIX timestamps for start and end of previous day this way.
NOTE – start and end in UTC time

var start = new Date().setUTCHours(0,0,0,0)-1000 * 60 * 60 * 24;
var end = new Date().setUTCHours(23,59,59,999)-1000 * 60 * 60 * 24;
console.log(start)
console.log(end)


console.log(new Date(start).toUTCString())
console.log(new Date(end).toUTCString())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading