Skip to content
Advertisement

sequelize compare date with date-timestamp

my simple use case is I pass a date and try to compare that with the default createdAt column.

where: {
                createdAt: {
                    $eq: date
                }
            }

and my date is a string like this date = '2018-12-12'

The problem here is sequlize not compare only the date. But it does add time 00:00:00 to my date and then compare. So the query sequlize generate is like this.

WHERE `redeem_points`.`createdAt` = '2018-11-02 00:00:00';

What I deserved

WHERE `redeem_points`.`createdAt` = '2018-11-02';

How do I achieve this using sequlize?

Advertisement

Answer

I think you would want something more like:

{
  where: {
    createdAt: { [Op.like]: `${date}%`, },
  }
}

Which would give SQL syntax like (note the wildcard):

WHERE createdAt LIKE '2018-11-02%'

Operators can give you a broad range of SQL syntax equivalents, additionally I think the shorthand you are using is deprecated so I subbed in the Op syntax you might need that as sequelize.Op if you aren’t destructuring your variables.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement