# DateRange Query in NEST

**URL:** <https://discuss.elastic.co/t/daterange-query-in-nest/66266>\
**Category:** Elasticsearch\
**Created:** [November 16, 2016, 4:34pm UTC](https://discuss.elastic.co/t/daterange-query-in-nest/66266 "2016-11-16T16:34:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars2](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Lars2](https://discuss.elastic.co/u/Lars2)\
**Post date:** [November 16, 2016, 4:34pm UTC](https://discuss.elastic.co/t/daterange-query-in-nest/66266/1 "2016-11-16T16:34:07Z")

</div>

I am trying to do a DateRangeQuery with NEST. This is my code:

> ```
> qbb.Bool(
> qbm =>
> qbm.Must(
> qbms =>
> qbms.DateRange(t => t.Field("DateField").GreaterThanOrEquals(DateMath.Now.Add(TimeSpan.FromDays(-8)).RoundTo(TimeUnit.Day)))))
> 
> ```

I am trying to select everything dat has a date greater than or equal to 8 days ago. But this code returns a useless error. Does anyone have a tip?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 21, 2016, 8:43am UTC](https://discuss.elastic.co/t/daterange-query-in-nest/66266/2 "2016-11-21T08:43:07Z")

</div>

What's the error message returned? Is it in the client or returned from Elasticsearch? More detail would be good here.

There is an implicit conversion from `DateTime` to `DateMath`, so instead of using

```auto
DateMath.Now.Add(TimeSpan.FromDays(8)).RoundTo(TimeUnit.Day)

```

which evaluates to "now+8d/d", you could use

```auto
DateTime.UtcNow.Date.AddDays(-8)

```

which will evaluate to a specific date .e.g. "2016-11-13T00:00:00Z" for today's date as `DateTime.UtcNow`

---

<div class="post-metadata">

**Author:** ![Lars2](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Lars2](https://discuss.elastic.co/u/Lars2)\
**Post date:** [November 21, 2016, 8:59am UTC](https://discuss.elastic.co/t/daterange-query-in-nest/66266/3 "2016-11-21T08:59:06Z")

</div>

Yes, i indeed solved it like you suggested. Thanks for the reply!

---

<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:** [December 19, 2016, 8:59am UTC](https://discuss.elastic.co/t/daterange-query-in-nest/66266/4 "2016-12-19T08:59:11Z")

</div>

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