# Retrieving a date range in c#

**URL:** <https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304>\
**Category:** Elasticsearch\
**Created:** [October 12, 2018, 9:24pm UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304 "2018-10-12T21:24:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![donElk](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@donElk](https://discuss.elastic.co/u/donElk)\
**Post date:** [October 12, 2018, 9:24pm UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/1 "2018-10-12T21:24:42Z")

</div>

Hello,  
I am trying to get a list of records from my Elasticsearch client using C#. Within my Elasticsearch index I have a field called startdate. I am looking to query a date range based off this code.

```
        List<Employee> respose = Connection.EsClient().Search<Employee>(s => s
                .Index("employee")
                .AllTypes()
                .From(0)

                .Query(q => q
                      .DateRange(c => c
                           .Field("startdate")
                           .Format("yyyy-MM-dd")
                           .GreaterThanOrEquals("2015/01/01")
                           .LessThanOrEquals("2015/01/15")
                       )
                )
           ).Documents.ToList();

```

When I run this code nothing returns back from Elasticsearch. Also, if I increase the date range it returns back everything which is incorrect. It is kind of like it is ignoring the DateRange filter. I am currently running Elasticsearch 6.4.2. Anything helps, thanks!

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [October 14, 2018, 2:57am UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/2 "2018-10-14T02:57:13Z")

</div>

Hi DonElk,

Would you be able to generate the JSON that is being sent for this query? It is difficult to diagnose any problems from the C# without seeing what the client library is actually sending.

---

<div class="post-metadata">

**Author:** ![donElk](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@donElk](https://discuss.elastic.co/u/donElk)\
**Post date:** [October 15, 2018, 5:32pm UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/3 "2018-10-15T17:32:54Z")

</div>

Raw:  
"{\r\n "from": 0,\r\n "size": 10,\r\n "query": {\r\n "range": {\r\n "startdate": {\r\n "\_name": "startDate\_query",\r\n "gte": "01/01/2016 11:25:06",\r\n "lte": "15/01/2016 11:25:06",\r\n "format": "dd/MM/yyyy hh:mm:ss||yyyy"\r\n }\r\n }\r\n }\r\n}"

Clean:  
"{"from": 0, "size": 10, "query": {"range": {"startdate": {"\_name": "startDate\_query", "gte": "01/01/2016 11:25:06", "lte": "15/01/2016 11:25:06", "format": "dd/MM/yyyy hh:mm:ss||yyyy"}}}}"

This is the same code, just different parameters used to call Elasticsearch Api. Thank you!

---

<div class="post-metadata">

**Author:** ![donElk](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@donElk](https://discuss.elastic.co/u/donElk)\
**Post date:** [October 15, 2018, 6:08pm UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/4 "2018-10-15T18:08:26Z")

</div>

By the way, I am also seeing this same behavior in Postman. For example:  
**Request:**  
{  
"query": {  
"range" : {  
"startdate" : {  
"format": "mm/dd/yyyy hh:mm:ss",  
"gte": "02/08/2016 11:25:06",  
"lt": "03/08/2016 11:25:06"  
}  
}  
}  
}

**Response:**  
"hits":[  
"startdate": "03/28/2016 13:26:50",  
"startdate": "03/05/2016 13:48:28",  
"startdate": "03/08/2016 13:25:56",  
"startdate": "03/02/2016 14:50:48",  
"startdate": "06/03/2016 13:24:01",  
"startdate": "11/08/2016 15:25:03"  
]

---

<div class="post-metadata">

**Author:** ![donElk](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@donElk](https://discuss.elastic.co/u/donElk)\
**Post date:** [October 15, 2018, 6:52pm UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/5 "2018-10-15T18:52:00Z")

</div>

Just realized the startdate 'type' is set to text. I will probably need to change this to date to fixed this issue. The index data was pulled from a SQL database which was parsed to Elasticsearch using Logstash. The data type for 'startdate' within SQL is of type datetime.

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [October 24, 2018, 2:27am UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/6 "2018-10-24T02:27:07Z")

</div>

That would definitely do it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 21, 2018, 2:27am UTC](https://discuss.elastic.co/t/retrieving-a-date-range-in-c/152304/7 "2018-11-21T02:27:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
