# Calculate Time Difference in Kibana

**URL:** <https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112>\
**Category:** Kibana\
**Created:** [January 29, 2019, 8:24am UTC](https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112 "2019-01-29T08:24:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi\_Purnama](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adi_purnama/32/53690_2.png) [@Adi\_Purnama](https://discuss.elastic.co/u/Adi_Purnama)\
**Post date:** [January 29, 2019, 8:24am UTC](https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112/1 "2019-01-29T08:24:29Z")

</div>

Hi  
I need help for new scripted field for this case:

started\_at: 2017-04-01 07:41:25  
ended\_at: 2017-05-01 10:14:34  
output: 26:33:09

I am using Kibana 6.4.2

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [January 29, 2019, 5:27pm UTC](https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112/2 "2019-01-29T17:27:19Z")

</div>

It's going to be faster to precompute this value and store it in the raw document, but if you need to do so using a scripted field, the following painless script will get you the difference in "day:hour:second" format:

```auto
def diffInMs = doc['ended_at'].value.getMillis() - doc['started_at'].value.getMillis();
def duration = Duration.ofMillis(diffInMs);
def hours = duration.toHours();
def minutes = duration.minusHours(hours).toMinutes();
def seconds = duration.minusHours(hours).minusMinutes(minutes).getSeconds();
def str = hours + ':' + minutes + ':' + seconds;
return str;

```

---

<div class="post-metadata">

**Author:** ![Adi\_Purnama](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adi_purnama/32/53690_2.png) [@Adi\_Purnama](https://discuss.elastic.co/u/Adi_Purnama)\
**Post date:** [January 31, 2019, 11:01am UTC](https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112/3 "2019-01-31T11:01:45Z")

</div>

Thanks Brandon

---

<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:** [February 28, 2019, 11:01am UTC](https://discuss.elastic.co/t/calculate-time-difference-in-kibana/166112/4 "2019-02-28T11:01:45Z")

</div>

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