如何在查询中的Sequelize中过滤日期类型

时间:2016-01-31 16:05:55

标签: sequelize.js

以下代码不起作用:

model.find({
  where: {
    id: 1,
    publishedAt: Sequelize.DATE
  }
})

我试图找到id = 1的模型,但前提是它的publishedAt值是一个日期。

http://docs.sequelizejs.com/en/latest/docs/querying/#where

1 个答案:

答案 0 :(得分:1)

好的,我最好的选择应该是在查询后过滤日期值。

//Finds out if the given value is a valid date. You can add some more specific checks.
var isDate = function(date) {
  return ( (new Date(date) !== "Invalid Date" && !isNaN(new Date(date)) ));
}

model.find({
  where: {
    id: 1,
    publishedAt: {
      $ne: null
    }
  }
}).
filter(function(record) {
  return isDate(record.publishedAt);
})