I'm pretty new with moment.js. I have a notification page and on load I would like to display my posts where someone commented. I only want to see the posts where someone commented today. Can someone tell where I went wrong?
I have the following code:
I want to show all the posts where comments were placed on the local start of the day. Then I want to transform this date to UTC time because the mongoDB comment document got saved in UTC time as well.
// Generate the actual time
const todayForEvent = moment().startOf('day')
.utc().format();
console.log('todayForEvent', todayForEvent);
const posts = await Post.find({
// From this user...
$and: [
// Find normal posts that has comments (recent interactions)
{ _posted_by: userId },
{ comments: { $exists: true, $ne: [] } },
{ 'comments.created_date': { $gte: todayForEvent } }
]
})
console.log(posts);
this is how I save the date of the comment with Mongoose. What I want to do is save it in UTC time
const CommentSchema = new Schema({
created_date: {
type: Date,
default: moment.utc().format()
}
});
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
I did two console.logs and I'll add a screenshot of my database document as well
console.log(todayForEvent)
returns 2019-01-02T06:00:00Z
console.log(posts)
returns an empty array, so no matches were found
Database screenshot: as you can see below the format is 2019-01-02 12:26:23.000