# Script field to subtract dates, compute time difference, not working

**URL:** <https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217>\
**Category:** Kibana\
**Created:** [January 19, 2018, 10:57am UTC](https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217 "2018-01-19T10:57:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijakret](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@sijakret](https://discuss.elastic.co/u/sijakret)\
**Post date:** [January 19, 2018, 10:57am UTC](https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217/1 "2018-01-19T10:57:47Z")

</div>

hi,

i am trying to compute a date difference in a scripted field

```
if(!doc['lastseen'].empty && !doc['firstseen'].empty) {
 return doc['lastseen'].value - doc['firstseen'].value
} else {
 return 0
}

```

first seen and last seen are date fields.  
When i go to discover i get the following error:

"Courier Fetch: 2 of 5 shards failed."

if i return 0 in the if statement all is fine. The same if i just return doc['firsteen'].value or doc['lastseen'].value.  
The field is set to type: number.

to me it seems to me that as soon as i start to do arithmetics on the fields values the discovery breaks.

---

<div class="post-metadata">

**Author:** ![sijakret](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@sijakret](https://discuss.elastic.co/u/sijakret)\
**Post date:** [January 19, 2018, 11:01am UTC](https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217/2 "2018-01-19T11:01:34Z")

</div>

the scripting language was set to painless.  
if i switch to "expression" and use a ternary expression it works 😕

```
(!doc['lastseen'].empty && !doc['firstseen'].empty) ?
 doc['lastseen'].value - doc['firstseen'].value
: 0
```

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [January 19, 2018, 3:56pm UTC](https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217/3 "2018-01-19T15:56:27Z")

</div>

If you want to do date math in Painless you'll need to cast the field values to date types first. There's a good blog post [here](https://www.elastic.co/blog/using-painless-kibana-scripted-fields) which includes some examples of working with dates, and other great Painless tips.

---

<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 16, 2018, 3:56pm UTC](https://discuss.elastic.co/t/script-field-to-subtract-dates-compute-time-difference-not-working/116217/4 "2018-02-16T15:56:46Z")

</div>

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