This page describes how you can search by date with date range search. It also describes the creation of date range search queries and the management of formats for date range search.
Creating date range search queries
Date range search allows you to search documents for dates from a given range written in given date formats.
Queries for date range search in text form are specified in the following format:
daterange( date ~~ date )
where date is the date in yyyy-MM-dd format, for example, 2019-09-13. Please note that in a search query, dates are always set in the same format.
The following example demonstrates how to search by date using queries in text and object form.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Search for dates using query in text formstringquery1="daterange(2017-01-01 ~~ 2019-12-31)";SearchResultresult1=index.Search(query1);// Search for dates using query in object formSearchQueryquery2=SearchQuery.CreateDateRangeQuery(newDateTime(2017,1,1),newDateTime(2019,12,31));SearchResultresult2=index.Search(query2);
Specifying date range search formats
To perform the date range search, the date templates specified in the search options are used. The following formats are used by default:
dd.MM.yyyy
MM/dd/yyyy
yyyy-MM-dd
Formats for date range search are set in the DateFormats property of the SearchOptions class. To add a format, use the Add method, to remove it, use the Remove method, to clear a collection of existing formats, use the Clear method. Each format in the collection is defined by an instance of the DateFormat class. To create a new format, you must specify a sequence of format elements and a date separator string. The sequence must contain at least two elements. All available format elements are represented in the DateFormatElement class:
DayOfMonth specifies the day of month element, represented by one or two digits;
DayOfMonthTwoDigits specifies the day of month element, represented by two digits;
Month specifies the month element, represented by one or two digits;
MonthTwoDigits specifies the month element, represented by two digits;
MonthAbbreviatedName specifies the month element, represented by abbreviated name;
MonthFullName specifies the month element, represented by full name;
Year specifies the year element, represented by one, two, three, or four digits;
YearTwoDigits specifies the year element, represented by two digits;
YearFourDigits specifies the year element, represented by four digits;
An example of setting the date format for the search is presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Setting date formatsSearchOptionsoptions=newSearchOptions();options.DateFormats.Clear();// Removing default date formatsDateFormatElement[]elements=newDateFormatElement[]{DateFormatElement.MonthTwoDigits,DateFormatElement.DateSeparator,DateFormatElement.DayOfMonthTwoDigits,DateFormatElement.DateSeparator,DateFormatElement.YearFourDigits,};// Creating a date format pattern 'MM/dd/yyyy'DateFormatdateFormat=newDateFormat(elements,"/");options.DateFormats.Add(dateFormat);// Searching in the index.// For the given query, for example, the date 09/27/2019 will be found,// but the date 2019-09-27 will not be found, because it is presented in a format that is not specified in the search options.SearchResultresult=index.Search("daterange(2017-01-01 ~~ 2019-12-31)",options);
DateFormat(DateFormatElement[] formatElements, string dateSeparator) creates a format with the specified date separator. Month names, if the format contains them, are taken from the current culture.
DateFormat(string cultureName, DateFormatElement[] formatElements) creates a format that uses the date separator and the month names of the specified culture, for example, “en-US” or “de-DE”. This constructor is useful for formats containing the MonthAbbreviatedName or MonthFullName elements.
The following example demonstrates the creation of a format with month names in German, for example, ‘27.September.2019’.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Creating a date format pattern 'dd.MMMM.yyyy' with the date separator and month names of the German cultureDateFormatElement[]elements=newDateFormatElement[]{DateFormatElement.DayOfMonthTwoDigits,DateFormatElement.DateSeparator,DateFormatElement.MonthFullName,DateFormatElement.DateSeparator,DateFormatElement.YearFourDigits,};DateFormatdateFormat=newDateFormat("de-DE",elements);// Adding the format to the default date formatsSearchOptionsoptions=newSearchOptions();options.DateFormats.Add(dateFormat);// Searching in the indexSearchResultresult=index.Search("daterange(2017-01-01 ~~ 2019-12-31)",options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: