Sails JS - Waterline ORM - only request date, not time

Looking for a date request, has anyone encountered this?

Code example:

////MODEL module.exports = { attributes: { date: { type: 'date', required: true } } }; ////CONTROLLER var today = moment().toISOString(); var queryObj = { date: today }; var newDay = { date: today }; Day.findOrCreate(queryObj, newDay).exec(function(err, day) { console.log(day) }); 

Obviously, this creates a new record with every update, since the iso line will change every second.

Thanks for the help!

+4
source share
1 answer

Instead of asking for a single date, you can request a date range that includes all of today's. First, you will need to create values ​​for this range - I whipped it with Moment, but probably the best way:

 var begin = moment(moment().format("YYYY-MM-DD")).toISOString(); var end = moment(moment().format("YYYY-MM-DD")).add(1, 'days').toISOString(); 

Then you can use query operators to search for a range:

 var queryObj = {date: {'>=': begin, '<': end}}; Day.findOrCreate(queryObj, newDay).exec(function(err, day) { console.log(day) }); 

As always, be aware of problems with the time zone!

+6
source

All Articles