# Time of Day filter

**URL:** https://discuss.elastic.co/t/time-of-day-filter/263277
**Category:** Kibana
**Created:** [February 4, 2021, 6:10pm UTC](https://discuss.elastic.co/t/time-of-day-filter/263277 "2021-02-04T18:10:15Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![stevezemlicka](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevezemlicka](https://discuss.elastic.co/u/stevezemlicka)
#### Post date: [February 4, 2021, 7:53pm UTC](https://discuss.elastic.co/t/time-of-day-filter/263277/4 "2021-02-04T19:53:08Z")

</div>

> [@Scripted field HourOfDay()](https://discuss.elastic.co/t/scripted-field-hourofday/87520):
>
> Hi, I want a field containing the hour of the day for each elasticsearch document. The easiest way would be to parse @timestamp into a hour\_of\_day field but that means I would have to reindex all my data. I saw another solution by using Scripted Fields with: doc["@timestamp"].getHourOfDay() That works to get the hour ('HH') in format 00-23 and that's exactly what I was expecting. But, the hour\_of\_day field isn't correct as it shows a different hour value (probably due to timezone difference…

I tried to create a scripted field for this using the above info but seem to be having difficulty.

> LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()

produces

> {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> " ^---- HERE"  
> ],  
> "script": "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> "lang": "painless",  
> "position": {  
> "offset": 62,  
> "start": 0,  
> "end": 110  
> }  
> }  
> ],  
> "type": "search\_phase\_execution\_exception",  
> "reason": "all shards failed",  
> "phase": "query",  
> "grouped": true,  
> "failed\_shards": [  
> {  
> "shard": 0,  
> "index": "san-isabel",  
> "node": "erbPD7dEQPKwU5NNOXVB9g",  
> "reason": {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> " ^---- HERE"  
> ],  
> "script": "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> "lang": "painless",  
> "position": {  
> "offset": 62,  
> "start": 0,  
> "end": 110  
> },  
> "caused\_by": {  
> "type": "wrong\_method\_type\_exception",  
> "reason": "cannot convert MethodHandle(Dates)JodaCompatibleZonedDateTime to (Object)long"  
> }  
> }  
> }  
> ]  
> }

and this:

> doc['@timestamp'].date.hourOfDay

produces

> {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "doc['@timestamp'].date.hourOfDay",  
> " ^---- HERE"  
> ],  
> "script": "doc['@timestamp'].date.hourOfDay",  
> "lang": "painless",  
> "position": {  
> "offset": 17,  
> "start": 0,  
> "end": 32  
> }  
> }  
> ],  
> "type": "search\_phase\_execution\_exception",  
> "reason": "all shards failed",  
> "phase": "query",  
> "grouped": true,  
> "failed\_shards": [  
> {  
> "shard": 0,  
> "index": "san-isabel",  
> "node": "erbPD7dEQPKwU5NNOXVB9g",  
> "reason": {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "doc['@timestamp'].date.hourOfDay",  
> " ^---- HERE"  
> ],  
> "script": "doc['@timestamp'].date.hourOfDay",  
> "lang": "painless",  
> "position": {  
> "offset": 17,  
> "start": 0,  
> "end": 32  
> },  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "Illegal list shortcut value [date]."  
> }  
> }  
> }  
> ]  
> }

---

_[View the full topic](https://discuss.elastic.co/t/time-of-day-filter/263277)._
