Filter by date: instantsearch

Hi,
I am trying to filter by date:
-Today
-Morning
-This month
-Another date
I use numericRefinementList, my question is how do I send these parameters to instantsearch? Or from there get the values ​​for today, tomorrow …
Thanks

Hi @mrrabbit

I think you could work something out with numericRefinementList that you mentioned.

Good news is that any Date object can be convert into a timestamp that’ll work just fine as a numerical value.
But I’m not sure I understand how you want to apply the filter. So here is a suggestion, please provide a bit more context if that’s not what you want to achieve.

Let’s say you have an index with two objects ;

[{
   eventName: "Search party", 
   date: "2017/09/01 9:42:00",
   timestamp; 1504251720 
 },
 {
   eventName: "Search party", 
   date: "2017/10/01 9:42:00",
   timestamp; 1506843720 
 }]

You could set up your widget like

search.addWidget(
  instantsearch.widgets.numericRefinementList({
    container: '#calendar',
    attributeName: 'timestamp',
    options: [
      {name: 'All'},
      {start: **[TS of today 00:00:00]** end:**[TS of today 23:59:59]**, name: 'Today'},
      {start: **[TS of today + 1 month ]**, name: 'In one month'},
      {start: **[TS of today- 1 month]**, end: **[Today]**, name: 'Happened last month'}
    ],
    templates: {
      header: 'Calendar'
    }
  })
);

Hope this helps :wink:

2 Likes

Hey, as @pierre.aurele.martin said, you should use a timestamp and get the value of the start of your date range ( tomorrow 0:00) to the end of your range (tomorrow 23:59), and then check with a numericRefinementList if it’s between those values.

If you need any more guidance, feel free to ask