# Add scripted field for finding difference between two dates

**URL:** https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031
**Category:** Kibana
**Created:** [June 15, 2018, 5:34am UTC](https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031 "2018-06-15T05:34:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ishantanu](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ishantanu](https://discuss.elastic.co/u/ishantanu)
#### Post date: [June 15, 2018, 5:34am UTC](https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031/1 "2018-06-15T05:34:59Z")

</div>

Hi,

I want to add a scripted field to calculate the difference between two dates - start date and end date. However, the format of them is a little bit different. Here's how they look like:

proc\_start\_time: 04:10:26  
proc\_end\_time: 04:10:29

I want to add another field called proc\_time which will calculate the difference between these two fields.

I've gone through lucene expressions and painless docs (not very good at them).

Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [June 15, 2018, 2:54pm UTC](https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031/2 "2018-06-15T14:54:25Z")

</div>

Are they saved as a date field in Elasticsearch? then it should be really easy:  
`doc['proc_end_time'].value - doc['proc_start_time'].value` will give you the value in miliseconds. If you set the custom format for the field to "Duration" you will also get a nice human readable format for that field.

---

<div class="post-metadata">

### Author: ![ishantanu](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ishantanu](https://discuss.elastic.co/u/ishantanu)
#### Post date: [June 16, 2018, 7:22am UTC](https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031/3 "2018-06-16T07:22:44Z")

</div>

This is not working, I already tried. After adding field when I go in discover tab to see data, it says:

`Courier Fetch: 5 of 179 shards failed.`

Just to clarify, it is not in date field. I don't want to replace `@timestamp` field.

Update:

Converted to date field and still not working @Marius_Dragomir

---

<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 14, 2018, 7:22am UTC](https://discuss.elastic.co/t/add-scripted-field-for-finding-difference-between-two-dates/136031/4 "2018-07-14T07:22:49Z")

</div>

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