6

Goal: Return a list of items that were created between two dates.

According to this issue https://github.com/balderdashy/waterline/issues/110 there is no between function just yet. However the work around is the following:

User.find({
    date: { '>': new Date('2/4/2014'), '<': new Date('2/7/2014') }
}).exec(/* ... */);

To be more exact, we don't want the hard coded dates above so we read in the input from a form submission like so:

    start = new Date(req.param('yearStart') + '/' + req.param('monthStart') + '/' + req.param('dayStart'));
    end = new Date(req.param('yearEnd') + '/' + req.param('monthEnd') + '/' + req.param('dayEnd'));

Printing start and end to console shows me this (different timezones for some reason)?

from: Sat Mar 01 2014 00:00:00 GMT-0500 (EST)
to: Sat Apr 30 2016 00:00:00 GMT-0400 (EDT)

However my view returns nothing every time.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Alexei Darmin
  • 2,079
  • 1
  • 18
  • 30

2 Answers2

14

While writing this question I realized the issue was that I had date instead of createdAt in my filter.

So the following works:

User.find({
    createdAt: { '>': start, '<': end }
}).exec(/* ... */);
Alexei Darmin
  • 2,079
  • 1
  • 18
  • 30
  • Do you know if there is a possibility to use this feature with date formats like **2018-04-11T12:45:00.000Z**? Currently it seems only to work with **2018-04-11** – FranzHuber23 Apr 11 '18 at 13:00
  • I've put the question here: https://stackoverflow.com/questions/49775927/using-between-query-for-full-date-format-in-sails-js-wateline – FranzHuber23 Apr 11 '18 at 13:05
1

If you are wondering how to use the API blueprint query, you will need to use the toISOString() method of the Date object. For example : http://localhost:1337/:model/?where={date: {'<=', date.toISOString()}}

Marrouchi
  • 271
  • 2
  • 4