Gravatar for chris.weiss@rbaconsulting.com

Question by chrisweiss, Jul 20, 2016 4:00 PM

Javascript queryBuilder expressions, filtering by date

(I'm using Coveo for Sitecore 4.0, June release, with Sitecore 8.1 Update 2)

I'm looking to filter results on a field in the index by date, specifically, I'm trying to show only results that have an Expiration Date (a Date/time field in the index) equal to today or in the future. However, I'm not sure what the format of the current date should be in order to compare properly.

This part of the expression looks like this right now: '(@(Model.ToCoveoFieldName("expiration_date")) >= ' + new Date().getDate() + ')'

I'm talking about the bolded part. I'm not sure if I should be using DateTime.Now, GETDATE(), or the js insertion I am now. OR maybe something entirely different.

Thanks!

2 Replies
Gravatar for olamothe@coveo.com

Answer by olamothe, Jul 20, 2016 4:14 PM

Hi,

the query syntax for date range is documented with some example here : https://onlinehelp.coveo.com/en/ces/7.0/user/usefulfieldquery_examples.htm

Example: @date>=now-1w finds all documents created or modified within the last week. 
@date=2010/01/01..2010/12/31 finds all documents created or modified within the year 2010.

As far as date formatting goes, you can either use pure js, or use a library to format it for you. I suggest using http://momentjs.com/

Gravatar for dlavoie@coveo.com

Answer by Daniel Lavoie, Jul 20, 2016 4:19 PM

Date expressions support keywords to ease the creation of temporal queries (today, yesterday and now).

today and yesterday will match on whole days, while now will match at the seconds level.

In your case, @datefield >= today will do what you want.

Ask a question