Search code examples
sails.jswaterline

Between Dates using Waterline ORM SailsJS


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.


Solution

  • 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(/* ... */);