# How to filter date field by days and hours separately

**URL:** <https://discuss.elastic.co/t/how-to-filter-date-field-by-days-and-hours-separately/265046>\
**Category:** Kibana\
**Created:** [February 22, 2021, 9:28am UTC](https://discuss.elastic.co/t/how-to-filter-date-field-by-days-and-hours-separately/265046 "2021-02-22T09:28:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hi\_u\_Nguy\_n\_Dang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hi_u_nguy_n_dang/32/78885_2.png) [@Hi\_u\_Nguy\_n\_Dang](https://discuss.elastic.co/u/Hi_u_Nguy_n_Dang)\
**Post date:** [February 22, 2021, 9:28am UTC](https://discuss.elastic.co/t/how-to-filter-date-field-by-days-and-hours-separately/265046/1 "2021-02-22T09:28:17Z")

</div>

Hi,  
I want to create a query that returns all the data from last 30 days and only between 09:00 AM - 18:00 PM.  
My date field contains both the date and the time of the day (For example "time" : "2021-02-06T06:40:55Z").  
Can I do something like or how can I achieve this with this mapping ?  
'  
'''  
"query" :{  
"bool": {  
"must": [  
{  
"range": {  
"time": {  
"gte": "2021-02-01",  
"lte": "2021-02-22"  
}  
}  
},  
{  
"range": {  
"time": {  
"gte": "09:00:00",  
"lte": "18:00:00"  
}  
}  
}  
]  
}  
}  
'''  
'

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 22, 2021, 10:40am UTC](https://discuss.elastic.co/t/how-to-filter-date-field-by-days-and-hours-separately/265046/2 "2021-02-22T10:40:58Z")

</div>

This sounds like a good use case for a [scripted field](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html). It allows you to calculate a "derived" value from a document (like the hour of day as a number from a date field).

It's quite common actually, the Logs sample data set includes this exact scripted field.

 ![Screenshot 2021-02-22 at 11.40.08](https://us1.discourse-cdn.com/elastic/original/3X/7/5/75507c103767a5f3dd9326bf5f05030e8bc22624.png)

Beware of timezones though, as the hour of day depends on the configured time zone it's easy to run into confusing scenarios.

---

<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:** [March 22, 2021, 10:41am UTC](https://discuss.elastic.co/t/how-to-filter-date-field-by-days-and-hours-separately/265046/3 "2021-03-22T10:41:37Z")

</div>

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