# How to fix this date range

**URL:** <https://discuss.elastic.co/t/how-to-fix-this-date-range/243065>\
**Category:** Elasticsearch\
**Created:** [July 29, 2020, 1:52pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065 "2020-07-29T13:52:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [July 29, 2020, 1:52pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065/1 "2020-07-29T13:52:11Z")

</div>

Hi,

Here is sample code from the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#ranges-on-dates):

```
GET /_search
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "now-1d/d",
        "lt": "now/d"
      }
    }
  }
}

```

I want to change it so that `now` only uses the date part and not the time.  
My usecase is to get previous day data for 24 hours e.g Between 00:00:00 and 23:59:59.  
In other words the range should be [28-07-2020 to 29-07-2020].

Is there any extension on `now` like `now.Date`?

I was looking at painless script but not sure how to implement a functionality similar to range.

This [answer](https://discuss.elastic.co/t/is-it-possible-to-make-date-range-queries-with-multiple-date-fields/76278) is very close but I am not able to find how to find current date only without time.

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [July 29, 2020, 2:34pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065/2 "2020-07-29T14:34:04Z")

</div>

I believe you can solve your case by supplying a **format** in the [range query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html).

---

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [July 29, 2020, 3:30pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065/3 "2020-07-29T15:30:51Z")

</div>

I am not sure if I am getting right results.

```
GET testindex/_search
{
  "_source": [
    "@timestamp"
  ],
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-1d/d",
        "lt": "now",
        "format": "yyyy-MM-dd"
      }
    }
  },
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "currentdate": {
      "script": {
        "lang": "painless",
        "inline": "def date = new Date();return date;"
      }
    }
  }
}

```

Sample result:

```
  {
	"_index" : "testindex",
	"_type" : "logevent",
	"_id" : "YwY78iu6thrnhsrwZ",
	"_score" : null,
	"_source" : {
	  "@timestamp" : "2020-07-30T01:17:49.1361052+10:00"
	},
	"fields" : {
	  "currentdate" : [
		"2020-07-29T15:17:50.524Z"
	  ]
	},
	"sort" : [
	  1596045645136
	]
  },

```

It should have returned events with date less than 29th. I think I am getting a bit crazy with all this UTC and local offset.

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [July 29, 2020, 3:42pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065/4 "2020-07-29T15:42:58Z")

</div>

> [@pk.241011](#):
>
> UTC and local offset.

Yes, this seems to be an UTC issue since 2020-07-30T01:17:49 +10:00 is the same as 2020-07-29T15:16:49 UTC - which is within the date range you wanted. So you'll need to handle that as well.

I haven't faced this issue myself, but perhaps you could try the **time\_zone** parameter mentioned in the date range documentation? Quoting:

> `time_zone`
> 
> (Optional, string) [Coordinated Universal Time (UTC) offset](https://en.wikipedia.org/wiki/List_of_UTC_time_offsets) or [IANA time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) used to convert `date` values in the query to UTC.

---

<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:** [August 26, 2020, 3:43pm UTC](https://discuss.elastic.co/t/how-to-fix-this-date-range/243065/5 "2020-08-26T15:43:00Z")

</div>

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