Parse.com - how can I create a request equal to createdAt by date only with javascript


I want to find data on the "createdAt" field, but I need to search only with the date (no time).

var d = new Date(); var query = new Parse.Query("TableName"); query.equalTo("createdAt", d); 

Naji

+5
source share
2 answers

What you basically want to create two dates:

  • date 0: 0: 0 times
  • date+1day at 0: 0: 0 times

Then do a search:

 query.greaterThanOrEqualTo('createdAt', date); query.lessThan('createAt', datePlusOne); 

This effectively gives you the range dateT0: 0: 0 - dateT23: 59: 59.99999 inclusive, but in a safe way

If you want to use pure JavaScript:

 // assuming date is the date/time to start from date.setHours(0, 0, 0, 0); // hours/min/sec/ms cleared var datePlusOne = new Date(date); datePlusOne.setDate(datePlusOne.getDate() + 1); 

You can also use the minutes archive to make your code easier to read / maintain. This library is also used on the server side at parse.com, although this is an older version.

 m1 = new moment(date); m1.startOf('day'); m2 = new moment(m1); m2.add(1, 'day'); // convert moment back to JavaScript dates date = m1.toDate(); var datePlusOne = m2.toDate(); 

Complete solution using moments:

 var d = new Date(); var query = new Parse.Query("TableName"); var start = new moment(d); start.startOf('day'); // from the start of the date (inclusive) query.greaterThanOrEqualTo('createdAt', start.toDate()); var finish = new moment(start); finish.add(1, 'day'); // till the start of tomorrow (non-inclusive) query.lessThan('createdAt', finish.toDate()); query.find.then(function(results) { // use results }); 
+16
source

If you are looking for results filtered by "created today", you can do this:

 var moment = require("moment"); var start = moment().sod() //Start of day var end = moment().eod() //End of day var query = new Parse.Query("myClass") query.greaterThanOrEqualTo("createdAt", start.format()); query.lessThan("createdAt", end.format()); query.find({...}); 

Of course, if you are looking for a longer period of time than today, you will go with Timothy's answer.

This code has been tested in Parse Cloud Code with Momentjs 1.7.2

+3
source

Source: https://habr.com/ru/post/1211733/


All Articles