Haikal Nashuha
Haikal Nashuha

Reputation: 3048

Meteor Query Based On Date

I search high and low but the only I can find is how to search based on a date range (e.g. from date A to date B) How can I find for specific date?

I did this, but somehow it is not correct:

    var diff = parseInt(searchKey);
    var delta = diff * 24 * 60 * 60 * 1000;

    if(diff!=NaN){
      var targetDate = new Date(Date.now() + delta);              
      //alert(targetDate.toString()); 
      selector['date'] = targetDate;
      Session.set('selector', selector);
      Session.set('isFiltered',true);         
    }

The targetDate is a valid date, and the searchKey would be returned as remaining days (i.e 7 more days or -7 if the date has passed). The selector will be then passed as a parameter to find() for searching the collection.

Too bad. It doesn't work as intended. Do I have to parse or format the javascript Date object before doing something like this as query selector { date: '2014-04-01 00:00:00:00}? Can't I simply pass the JS object and the framework is smart enough to compare it?

Upvotes: 0

Views: 163

Answers (2)

Micha Roon
Micha Roon

Reputation: 4007

in order to get this to work you should use the moment package mrt add moment

when you create your document in MongoDB, you assign the ate as moment(moment().format("DD.MM.YYYY"), "DD.MM.YYYY").toDate() this will return the current date at midnight

when you query you can use the same format to search for a particular day. Use moment's excellent API to compute the correct date. 7 days in the future would be moment(moment().format("DD.MM.YYYY"), "DD.MM.YYYY").add('days', 7)

It's a bit cumbersome but the queries will be faster than searching for midnight to midnight.

Hope that helps

Upvotes: 1

Hubert OG
Hubert OG

Reputation: 19544

The date object contains both date and time values. To find items with date equal to a given day you need to search for items between beginning and end of said day. Looking for items with equal date will only show those where time is exactly equal, up to milliseconds.

Upvotes: 1

Related Questions