node.js - 如何在nodejs中比较两个日期

标签 node.js mongodb mongoose

我正在尝试获取 30 天前订阅的用户列表,我使用 $lt 来比较日期,但我什么也没得到,请提供帮助!

  • 这是我的架构模型:
var VisitorsSchema = new Schema({
    firstname: String,
    lastname: String,
    subDate: Date
}, {
    collection: 'users'
});

  • 这就是我试图让用户在 30 天之前订阅的方法:
users.find({
    "subDate": {
        $lt: new Date(new Date().setDate(new
            Date().getDate() - 30))
    }
}, function (err, resp) {
    if (err) {
        return next(err);
    }
    console.log(resp);
    res.send(resp);
});

注意:当我得到一个用户并比较他的订阅日期时,如下所示:

res.send(resp[0]['subDate']>de);

它工作正常,我得到了正确的结果(真或假)。

最佳答案

通过添加 toISOString() 解决了问题:

$lte: new Date(new Date().setDate(new Date().getDate()-daysNumber)).toISOString()

关于node.js - 如何在nodejs中比较两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809982/

相关文章:

node.js - AWS Elastic Beanstalk : Do I have to deploy my app with full "/node_modules" and "/bower_components" and compiled sass and other dependencies?

node.js - 这在 NodeJS 中是未定义的

node.js - 为什么express的sendFile函数将304视为错误?

node.js - MongoDb 查询匹配并删除数组中的元素?

c# - MongoDB 从子文档中删除子文档文档

javascript - 使用 mongoose 在 mongodb 中查找用户,并将结果返回为 JSON

javascript - 如何正确调用嵌套在 waterfall 函数中的异步 forEachOf 函数

mongodb - 将字段添加到MongoDB内部对象

node.js - 使用 Mongoose、Express 删除用户

node.js - 如何在同一个语句中使用填充和聚合?