# Extracting hour from date field (scripted field)

**URL:** <https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691>\
**Category:** Kibana\
**Created:** [May 14, 2015, 2:34pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691 "2015-05-14T14:34:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![whittssg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whittssg/32/27669_2.png) [@whittssg](https://discuss.elastic.co/u/whittssg)\
**Post date:** [May 14, 2015, 2:34pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/1 "2015-05-14T14:34:59Z")

</div>

Hello,

Is there a way to extract the hour from the timestamp field using a scripted field? I saw a blog post stating that this could be done a while back but it looks like it used groovy which is no longer supported.. is there a way to do it on the latest release? At the moment i am creating this field during the import but it is hard to get it to work for different timezones ( i have to create a field for each zone that i know will be used and then make a different visualization for each of those fields)

Also is there a way (using scripted fields) to get time duration in minutes from two different timedate fields?  
Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid/32/12566_2.png) [@rashid](https://discuss.elastic.co/u/rashid)\
**Post date:** [May 14, 2015, 4:56pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/2 "2015-05-14T16:56:29Z")

</div>

This is not currently possible, we're working to bring date support into scripts.

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [May 14, 2015, 4:57pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/3 "2015-05-14T16:57:56Z")

</div>

> [@whittssg](#):
>
> Is there a way to extract the hour from the timestamp field using a scripted field?

Currently, no. We supported groovy scripting in the early Kibana 4 betas but since it is no longer enabled by default we have dropped support. We expect the lucene expression syntax to grow over time though, and might someday support some form of date transformation. For now though you should use something (maybe logstash) to preprocess your documents and add the hour as it's own field.

> [@whittssg](#):
>
> is there a way (using scripted fields) to get time duration in minutes from two different timedate fields?

You can subtract two timestamps to get the number of seconds between them, but not the number of minutes.

---

<div class="post-metadata">

**Author:** ![whittssg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whittssg/32/27669_2.png) [@whittssg](https://discuss.elastic.co/u/whittssg)\
**Post date:** [May 14, 2015, 7:31pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/4 "2015-05-14T19:31:35Z")

</div>

OK thank you. Looking forward to the next release (or one after that)

---

<div class="post-metadata">

**Author:** ![Mahdy\_S](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahdy\_S](https://discuss.elastic.co/u/Mahdy_S)\
**Post date:** [September 15, 2016, 8:34am UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/5 "2016-09-15T08:34:23Z")

</div>

Hello Rashid,

This should be suopported now as per the following link:

> **[Kibana 4 Beta 3: Now More Filtery
	  	 | Elastic](https://www.elastic.co/blog/kibana-4-beta-3-now-more-filtery)**
>
> Kibana 4 Beta 3 is out! Along with the usual smattering of small fixes come a few knockout headliners. Once again, we offer you the choice to dive right in by grabbing it here: download Kibana 4 Beta ...

However, I have tried adding a scripted field as in the forementioned link in order to extract hour of the day, i.e. using the following script:  
**Integer.parseInt(new Date(doc["@timestamp"].value).format("H"))**  
but I got an error that the scripted field cannot be evaluated. Do you know what the problem is? I am using Kibana version 4.5.1

---

<div class="post-metadata">

**Author:** ![Nuno\_Boavida](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Nuno\_Boavida](https://discuss.elastic.co/u/Nuno_Boavida)\
**Post date:** [March 31, 2017, 3:02pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/6 "2017-03-31T15:02:24Z")

</div>

Hello Mahdy, for the record, i managed to accomplish that using the following expression

**Integer.parseInt(new SimpleDateFormat("HH").format(new Date(doc["created\_on"].value)))**

Regards

---

<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:** [July 6, 2017, 1:31pm UTC](https://discuss.elastic.co/t/extracting-hour-from-date-field-scripted-field/691/8 "2017-07-06T13:31:35Z")

</div>


