# Date range search This article describes how to search by date with date range search and how to specify date formats. 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#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Search for dates using query in text form string query1 = "daterange(2017-01-01 ~~ 2019-12-31)"; SearchResult result1 = index.Search(query1); // Search for dates using query in object form SearchQuery query2 = SearchQuery.CreateDateRangeQuery(new DateTime(2017, 1, 1), new DateTime(2019, 12, 31)); SearchResult result2 = index.Search(query2); ``` Since version 26.11, a date range is searched as sets: every part of the dates of the range, a day, a month or a year, is read from the index once, and a date is found in the text as its parts that follow one another. Therefore, the limits of the occurrences ([MaxTotalOccurrenceCount](https://reference.groupdocs.com/search/net/groupdocs.search.options/searchoptions/maxtotaloccurrencecount/) and [MaxOccurrenceCountPerTerm](https://reference.groupdocs.com/search/net/groupdocs.search.options/searchoptions/maxoccurrencecountperterm/) of the search options) count an occurrence of a part of a date once, as they count the occurrences of any word, and once for each chunk of a search by chunks. Before, an occurrence of a part was counted again for every date that had the part, in every format, so a range of a year or more could reach the default limit of the occurrences, and the later dates of the range were not found. ## 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](https://reference.groupdocs.com/search/net/groupdocs.search.options/searchoptions/dateformats/) property of the [SearchOptions](https://reference.groupdocs.com/search/net/groupdocs.search.options/searchoptions/) class. To add a format, use the [Add](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatcollection/add/) method, to remove it, use the [Remove](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatcollection/remove/) method, to clear a collection of existing formats, use the [Clear](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatcollection/clear/) method. Each format in the collection is defined by an instance of the [DateFormat](https://reference.groupdocs.com/search/net/groupdocs.search.options/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](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/) class: * [DayOfMonth](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/dayofmonth/) specifies the day of month element, represented by one or two digits; * [DayOfMonthTwoDigits](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/dayofmonthtwodigits/) specifies the day of month element, represented by two digits; * [Month](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/month/) specifies the month element, represented by one or two digits; * [MonthTwoDigits](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/monthtwodigits/) specifies the month element, represented by two digits; * [MonthAbbreviatedName](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/monthabbreviatedname/) specifies the month element, represented by abbreviated name; * [MonthFullName](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/monthfullname/) specifies the month element, represented by full name; * [Year](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/year/) specifies the year element, represented by one, two, three, or four digits; * [YearTwoDigits](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/yeartwodigits/) specifies the year element, represented by two digits; * [YearFourDigits](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/yearfourdigits/) specifies the year element, represented by four digits; * [DateSeparator](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/dateseparator/) specifies the date separator element. An example of setting the date format for the search is presented below. **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Setting date formats SearchOptions options = new SearchOptions(); options.DateFormats.Clear(); // Removing default date formats DateFormatElement[] elements = new DateFormatElement[] { DateFormatElement.MonthTwoDigits, DateFormatElement.DateSeparator, DateFormatElement.DayOfMonthTwoDigits, DateFormatElement.DateSeparator, DateFormatElement.YearFourDigits, }; // Creating a date format pattern 'MM/dd/yyyy' DateFormat dateFormat = new DateFormat(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. SearchResult result = index.Search("daterange(2017-01-01 ~~ 2019-12-31)", options); ``` The [DateFormat](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformat/) class has two constructors: * DateFormat(DateFormatElement[] formatElements, string dateSeparator) creates a format with the specified date separator. Since version 26.11, the dates of such a format are formatted in the invariant culture, whatever the current culture is: with the Gregorian calendar and the English names of the months. The default formats are created in the same way. Before, such a format took the calendar and the month names of the current culture, so on a computer whose culture has another calendar the default formats did not find the dates of the Gregorian calendar. * DateFormat(string cultureName, DateFormatElement[] formatElements) creates a format that uses the calendar, 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](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/monthabbreviatedname/) or [MonthFullName](https://reference.groupdocs.com/search/net/groupdocs.search.options/dateformatelement/monthfullname/) elements, and for searching the dates of a culture whose calendar is not the Gregorian one. Since version 26.11, the names of the months are found in any letter case, for example 'September' and 'september'. Before, a date with a capitalized name of a month, as in English or German, was not found. The following example demonstrates the creation of a format with month names in German, for example, '27.September.2019'. **C#** ```csharp string indexFolder = @"c:\MyIndex\"; string documentsFolder = @"c:\MyDocuments\"; // Creating an index in the specified folder Index index = new Index(indexFolder); // Indexing documents from the specified folder index.Add(documentsFolder); // Creating a date format pattern 'dd.MMMM.yyyy' with the date separator and month names of the German culture DateFormatElement[] elements = new DateFormatElement[] { DateFormatElement.DayOfMonthTwoDigits, DateFormatElement.DateSeparator, DateFormatElement.MonthFullName, DateFormatElement.DateSeparator, DateFormatElement.YearFourDigits, }; DateFormat dateFormat = new DateFormat("de-DE", elements); // Adding the format to the default date formats SearchOptions options = new SearchOptions(); options.DateFormats.Add(dateFormat); // Searching in the index SearchResult result = 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: * [GroupDocs.Search for .NET examples](https://github.com/groupdocs-search/GroupDocs.Search-for-.NET) * [GroupDocs.Search for Java examples](https://github.com/groupdocs-search/GroupDocs.Search-for-Java) ### Free online document search App Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online [Free Online Document Search App](https://products.groupdocs.app/search).