Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 611
Alexei Darmin Avatar asked Mar 29 '15 20:03

Alexei Darmin


2 Answers

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(/* ... */);
like image 84
Alexei Darmin Avatar answered Oct 23 '22 22:10

Alexei Darmin


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()}}

like image 1
Marrouchi Avatar answered Oct 23 '22 21:10

Marrouchi