Reputation: 924
Is possible to sort a ParseQuery by day/month/year and ignore their hours and minutes using Parse Javascript SDK?
I need to load registers ordened by some date column, but the hours and minutes to this query are irrelevant. How can i do this?
I need it because I'm applying two orders: date and likesCount. My app should list the objects by date (just day, month and year) and by likesCount, but if I use the time (hours and minutes) my query lose the objective.
Upvotes: 0
Views: 580
Reputation: 62676
First add an no-hour version of the date attribute to each object. beforeSave
can be used to maintain this without cluttering any other app logic...
Parse.Cloud.beforeSave("MyClass", function(request, response) {
var object = request.object;
var date = object.get("theDateAttribute");
object.set("dateOnlyAttribute", date.setHours(0,0,0,0));
response.success();
});
With this, each date is forced to be equivalent to the others with respect to time of day. Now you can sort secondarily on something else like this...
query.ascending("dateOnlyAttribute","likesCount");
Upvotes: 1
Reputation: 3441
I would do something like:
var myObject = Parse.Object.extend("MyObject");
var query = new Parse.Query(myObject);
query.ascending("MyDateField"); //or decending
query.find().then(function(objects) {
//logic
}, function(error) {
//error handle
});
Where MyDateField
is whatever your date is on your object. It would still sort with the date and time, but if the time is irrelevant then does it matter if it sorts by those?
Reference: https://parse.com/docs/js/guide#queries-query-constraints
Upvotes: 1