how compare a datetime in sails.js Model? here it is what i did but without luck.
var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';
Game.find({
where:{
active: true,
start: {
'>=' : _date
}
},
limit: 1,
sort: 'start asc'
}, function(err, game) {
console.log('ERROR: ' + err);
console.log('Game OBJ' + game.toString());
});
The datetime
type is transformed into an actual Javascript date in Waterline (the Sails ORM). So it needs to be compared against a date object, not a string. You can keep your code exactly as it is, but change the first line to:
var _date = new Date(moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z');
Then your query should find all Game
s that start in the future.
Of course since your code is just returning current date, you don't really need moment
at all:
var _date = new Date();
will work just as well.
var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';
you need to convert above string variable to date variable using below line
var tempDate = new Date(_date);
Now use $gte instead of '>='
Game.find({
where:{
active: true,
start: {
$gte : tempDate
}
},
limit: 1,
sort: 'start asc'
}, function(err, game) {
console.log('ERROR: ' + err);
console.log('Game OBJ' + game.toString());
});
You can also use $gt, $lt, $lte.
来源:https://stackoverflow.com/questions/23527136/cant-find-records-in-waterline-by-date-time