101

I am using Sequelize as my backend ORM. Now I wish to do some WHERE operations on a Date.

More specifically, I want to get all data where a date is between now and 7 days ago.

The problem is that the documentation does not specify which operations you can do on Datatypes.DATE

Can anyone point me in the right direction?

Ronald Martin
  • 4,278
  • 3
  • 27
  • 32
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • 3
    So have you tried $lt: (new Date ()) and $gt : (...) replace ... with time now - 7 days – Molda Apr 22 '15 at 16:21

5 Answers5

128

Just like Molda says, you can use $gt, $lt, $gte or $lte with a date:

model.findAll({
  where: {
    start_datetime: {
      $gte: moment().subtract(7, 'days').toDate()
    }
  }
})

If you're using v5 of Sequelize, you've to include Op because the key was moved into Symbol

const { Op } = require('sequelize')

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})

See more sequelize documentation here

Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
Evan Siroky
  • 9,040
  • 6
  • 54
  • 73
  • 7
    do note that this will not work from Sequelize 5... you can check out the breaking changes from Sequelize due to string based operators being insecure .. you should instead use [Op.gte] as per other comment – Stanley Aug 19 '19 at 01:04
  • Or `db.Sequelize.Op` if you just import db –  Mar 08 '20 at 23:53
  • 1
    HI, instead using moment().subtract(7, 'days').toDate(), i need to use column name createdDate . is that possible? – Anoop P S Jul 10 '20 at 11:44
74

I had to import the Operators symbols from sequelize and use like so.

const { Op } = require('sequelize')

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})

According to the docs, for security reasons this is considered best practise.

See http://docs.sequelizejs.com/manual/tutorial/querying.html for more info.

Using Sequelize without any aliases improves security. Some frameworks automatically parse user input into js objects and if you fail to sanitize your input it might be possible to inject an Object with string operators to Sequelize.

(...)

For better security it is highly advised to use Sequelize.Op and not depend on any string alias at all. You can limit alias your application will need by setting operatorsAliases option, remember to sanitize user input especially when you are directly passing them to Sequelize methods.

Pac0
  • 21,465
  • 8
  • 65
  • 74
James Graham
  • 741
  • 5
  • 3
  • 3
    Welcome to Stack Overflow ! To further improve the quality of your answer, keep in mind that links to other websites can become unavailable, and thus it is strongly advised that you copy the relevant info from the external documentation as a citation in the body of your answer. – Pac0 Nov 27 '17 at 11:16
  • This was the issue for me. Thanks. – user3386826 Aug 20 '19 at 00:37
  • This is the new standard for Sequelize. – Justin Rice Sep 20 '19 at 14:19
20

You can also use Sequelize.literal() to perform dates manipulation in SQL.

The following code works with Postgres, but I'm quite sure something similar could be done in other systems as well:

model.findAll({
  where: {
    start_datetime: {
      $gte: Sequelize.literal('NOW() - INTERVAL \'7d\''),
    }
  }
})

misterioss
  • 431
  • 2
  • 4
  • 13
marcopeg
  • 1,028
  • 10
  • 15
18

This solution is without the moment.js library.

Between 7 days ago and now

const sevenDaysAgo = new Date(new Date().setDate(new Date().getDate() - 7));
models.instagram.findAll({
  where: {
    my_date: {
      $gt: sevenDaysAgo,
      $lt: new Date(),
    },
  },
});

Between now and 7 days from now

const sevenDaysFromNow = new Date(new Date().setDate(new Date().getDate() + 7));
models.instagram.findAll({
  where: {
    my_date: {
      $gt: new Date(),
      $lt: sevenDaysFromNow,
    },
  },
});

Notes:

  • $gt stands for "greater than". You could use $gte instead of $gt. $gte stands for "greater than or equal to". Same for $lte of course.
  • Technically speaking, you need both $lt and $gt to make sure that the date isn't into the future (per the original question).
  • Other answers show the use of [Sequelize.Op.gt] instead of $gt. Use that if on Sequelize v5.
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    thank you i was scrolling and scrollin hoping for a non moment.js solution :) this need more upvotes – Qamar Stationwala Nov 19 '21 at 06:59
  • 1
    Upvoted. There seems to be no compelling reason to use `moment`. https://sequelize.org/v5/manual/querying.html now gives a non-moment example: `{ createdAt: { [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000) }` – NULL pointer Feb 15 '22 at 05:14
3

Based on the other answers and comments, a way without using third party packages would be:

const sevenDaysFromNowResults = await db.ModelName.findAll({
      where: {
        createdAt: {
          [Sequelize.Op.gte]: new Date(new Date() - (7 * 24 * 60 * 60 * 1000)) // seven days ago
        }
      }
    })

The seven days are calculated using Date utility in milliseconds (days * 24hrs * 60mins * 60secs * 1000ms)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
briancollins081
  • 845
  • 9
  • 19