# Calculate Difference between 2 Dates in Scipted Field

**URL:** <https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129>\
**Category:** Kibana\
**Created:** [October 19, 2018, 7:43am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129 "2018-10-19T07:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/groverjatin17/32/34657_2.png) [@groverjatin17](https://discuss.elastic.co/u/groverjatin17)\
**Post date:** [October 19, 2018, 7:43am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129/1 "2018-10-19T07:43:13Z")

</div>

Hi All,

I am trying to create a new scripted field by calculating the difference(in seconds) between 2 date in the following format `

> October 15th 2018, 22:15:27.823

The two fields I am using are start\_time and end\_time.

I have tried various method covering both Painless and expression as language.  
Every time I try I get different error.

Example:-

```
if(!doc['end_time'].empty)
{
return doc['end_time'].value - doc['start_time'].value
}

```

Error it gave is:-

> "caused\_by":{"type":"parse\_exception","reason":"unexpected character '{' on line (1) position (26)","caused\_by":{"type":"lexer\_no\_viable\_alt\_exception","reason":null}}}}]},"status":500}

Scenario 2:-  
if(!doc['end\_time'].empty){  
doc['end\_time'].date.secondOfMinute - doc['start\_time'].date.secondOfMinute}

Error it gave is:-  
"

> caused\_by":{"type":"parse\_exception","reason":"unexpected token 'x' on line (1) position (4) was expecting one of "}}}]},"status":500 Preformatted text at [http://10.23.213.99:5602/bundles/commons.bundle.js?v=16627:1:713755](http://10.23.213.99:5602/bundles/commons.bundle.js?v=16627:1:713755)

I also tried to follow this:

```
**doc['end_time'].value - doc['start_time'].value**

```

\*\* type: number\*\*  
\*\* Format: Duration\*\*  
\*\* InputFormat: seconds \*\*  
\*\* OutputFormat: Human Readable\*\*

Error was:-  
"

> caused\_by":{"type":"class\_cast\_exception","reason":"Cannot apply [-] operation to types [org.joda.time.MutableDateTime] and [org.joda.time.MutableDateTime]."}}}]},"status":500}

Please let me know the solution with some code. I will greatly appreciate your help.  
Thank you

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/groverjatin17/32/34657_2.png) [@groverjatin17](https://discuss.elastic.co/u/groverjatin17)\
**Post date:** [October 19, 2018, 8:58am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129/2 "2018-10-19T08:58:38Z")

</div>

Hi All,

Finally found what worked for me.

```
**if(!doc['end_time'].empty){**
**return doc['end_time'].value.getMillis() - doc['start_time'].value.getMillis();**
 **}**

```

Note:-  
Language:-Painless  
Type:-Number  
Format:-Durations  
Input Parameters:- milliseconds  
OutputParameter:- seconds  
Decimal:-2

Popularity is 1. Don't know what it does though.

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [October 19, 2018, 3:58pm UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129/3 "2018-10-19T15:58:08Z")

</div>

Popularity just prioritizes that field in the field list on the left in Discover.

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/groverjatin17/32/34657_2.png) [@groverjatin17](https://discuss.elastic.co/u/groverjatin17)\
**Post date:** [October 22, 2018, 9:11am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129/4 "2018-10-22T09:11:31Z")

</div>

Okay Thank you 🙂

I have asked 1 more topic in Kibana.  
Can you please help me with that?

---

<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:** [November 19, 2018, 9:11am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/153129/5 "2018-11-19T09:11:31Z")

</div>

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