# Calculate duration between two events using scripted fields

**URL:** https://discuss.elastic.co/t/calculate-duration-between-two-events-using-scripted-fields/81814
**Category:** Kibana
**Created:** [April 10, 2017, 12:21pm UTC](https://discuss.elastic.co/t/calculate-duration-between-two-events-using-scripted-fields/81814 "2017-04-10T12:21:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Schwerdtner](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Robert\_Schwerdtner](https://discuss.elastic.co/u/Robert_Schwerdtner)
#### Post date: [April 10, 2017, 12:21pm UTC](https://discuss.elastic.co/t/calculate-duration-between-two-events-using-scripted-fields/81814/1 "2017-04-10T12:21:30Z")

</div>

Hello is it possible to calculate the duration between two events (Dates) while using scripted fields and painless? I tried a lot but I can't get it working.

---

<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: [April 10, 2017, 2:38pm UTC](https://discuss.elastic.co/t/calculate-duration-between-two-events-using-scripted-fields/81814/2 "2017-04-10T14:38:51Z")

</div>

Yep, it can be done.  
If you have two date fields (time1 and time2) you can write a script like this:  
`doc['time2'].value - doc['time1'].value`  
You'll also want to have the scripted field with  
`type: number`  
`Format: Duration`  
`InputFormat: seconds` (if you use the date fields as normal UNIX timestamp)  
`OutputFormat: Human Readable`

---

<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: [May 8, 2017, 2:50pm UTC](https://discuss.elastic.co/t/calculate-duration-between-two-events-using-scripted-fields/81814/3 "2017-05-08T14:50:36Z")

</div>

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