0

I'm trying to filter some information by date and in my database the dates are saved with this format:

2018-05-23T23:00:00.000+00:00

this is what I tried to do

router.get('/byDate', function (req, res) {
    req.query.fromDate
    req.query.toDate

    Schedule.find({ date : { 
        $lt: new Date(req.query.toDate).toISOString(), 
        $gte: new Date(req.query.fromDate).toISOString()
      } } , function(err, data){
        if(err) throw err;
        res.json(data);
    });

});     
deHaar
  • 17,687
  • 10
  • 38
  • 51
Bar Levin
  • 205
  • 1
  • 12

3 Answers3

0

you can use the JavaScript Date Reference to get info from your timestamp. Create a Date Object and pass the timestamp and you can use many properties offered by Date Object.

for more info check: https://www.w3schools.com/jsref/jsref_obj_date.asp

Hope this helps.

router.get('/byDate', function (req, res) {
    req.query.fromDate
    req.query.toDate

    let fromdate = new Date(Date.parse(req.query.fromDate));

    let month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    console.log('Day --->' + fromdate.getDay());
    console.log('Month -->' + month[fromdate.getMonth()]);
    console.log('Year -->' + fromdate.getFullYear());

});  
Sachi.Dila
  • 1,126
  • 7
  • 15
0

The format that you are specifying can be obtained by converting the date to ISO string. However, you will need to perform a few more operations to get this particular desired format.

For Instance: I tried this. this.date = new Date(); this.formattedDate = this.date.toISOString();

The output was Wed Jul 24 2019 12:08:15 GMT+0530 (India Standard Time) 2019-07-24T06:38:15.837Z

You can display the time by passing it to datetime pipe and specifying the format. If you want to perform any additional operations on it, you can do so over this object using the link mentioned in the other answer (https://www.w3schools.com/jsref/jsref_obj_date.asp).

Ankit Joshi
  • 183
  • 1
  • 3
  • 9
0

Well I tried to slice it like that and it worked

 $lt: new Date(req.query.toDate).toISOString().slice(0,21), 
 $gte: new Date(req.query.fromDate).toISOString().slice(0,21)
Bar Levin
  • 205
  • 1
  • 12