How can i create relative-date/time filter in Algolia (i.e. "a day ago", "2 days ago", "3 days ago")?

237 Views Asked by At

In my hits, i have a prop that contains added .. and when i try to make a filter on date added, list of common date appears. How can I make a filter that says.. 1 day ago, 2 days ago, 1 week ago etc.. then filter the results.

so instead of showing full list of dates as filter, i can just personalize it depending on what i want.

Script:

//Algolia Widget for Date Added.
search.addWidget(
  instantsearch.widgets.menu({
    container: '#added-menu',
    attributeName: 'added',
    limit: 10,
    templates: {
      header: 'Added'
    }
  })
);

//Date added will display the whole list of common dates. 
1

There are 1 best solutions below

0
On

Update : I was able to find solution for handling the relative date and it is by the use filter named numericSelector found in algolia documentation. the situation is, i just need to copy the relative dates we have in our old App.

here are the constraints :

  • Use a relative time/date filter.
  • Do not display the common dates in the hits.
  • Use a dropdown.

    search.addWidget(
      instantsearch.widgets.numericSelector({
       container: '#added-menu',
        attributeName: 'added',
        templates: {
          header: 'Added'
        },
        operator: '>=',
        options: [
          {label: 'Anytime', value: 0 },
          {label: 'Today', value: daysBefore(1) },
          {label: 'Within 3 Days', value: daysBefore(3)},
          {label: 'Within 1 week', value: daysBefore(7)},
          {label: 'Within 2 weeks', value: daysBefore(14)},
          {label: 'Within 1 Month', value: daysBefore(30)},
          {label: 'Within 3 Months', value: daysBefore(90)},
          {label: 'Within 6 Months', value: daysBefore(183)}
        ]
      })
    );

daysBefore() is a function that returns the current date minus the number of days and then converted to a linux timestamp.