Compare date (moment.js) in MongoDB

I want to compare the date with MongoDB and my date.

I also read this and this , and I did not find the answer.

My code is:

today: function() { var today = moment().format(); return Posts.find({createdAt : { $gte : today}}) // show posts created in "future" , so this function must return nothing }, createdAt = moment().format();// in MongoDB 

As a result, this construct does not work, but if I compare this:

 var today = moment().format(); var daystart = moment().startOf('day').format(); if (daystart > today){ console.log ("YES"); } else if (daystart < today)console.log ("NO"); 

Return

 "NO" 

Can anybody help?

EDIT:

  today: function() { var today = moment().toDate(); var daystart = moment().startOf('day').toDate(); // console.log(today + daystart); return Posts.find({createdAt : { $gt : today}}) }, week: function() { var today = new Date(); return Posts.find({createdAt : { $lt : today}}) }, month: function() { var today = new Date(); return Posts.find({createdAt : { $ne : today}}) } createdAt = new Date(); 
+7
mongodb momentjs
source share
1 answer

.format() is a helper display function that returns a representation of a date string based on a passed token argument. To compare a date with MongoDB with the current date and time, just call moment() without parameters, without .format() and get your own Date object, which Moment.js wraps by calling toDate() :

 today: function() { var now = moment().toDate(); return Posts.find({createdAt : { $gte : now }}); } 
+11
source share

All Articles