# Painless script to get date difference

**URL:** <https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [March 13, 2020, 7:45am UTC](https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461 "2020-03-13T07:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Purushoth\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/purushoth_kumar/32/54579_2.png) [@Purushoth\_Kumar](https://discuss.elastic.co/u/Purushoth_Kumar)\
**Post date:** [March 13, 2020, 7:45am UTC](https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461/1 "2020-03-13T07:45:36Z")

</div>

Hi Team,

I want to calculate time difference between two times (incident created time & incident updated time fields).  
I'm trying to create a scripted field for this...but getting error while loading.

My scripts looks like this

doc['incident\_update\_timestamp'].value - doc['wit\_cell\_16\_Value'].value;

I'm getting the following error:

Error: Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"runtime error","script\_stack":["return doc['incident\_update\_timestamp'].value - doc['wit\_cell\_16\_Value'].value;"," ^---- HERE"],"script":"return doc['incident\_update\_timestamp'].value - doc['wit\_cell\_16\_Value'].value;","lang":"painless"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"soc3d-incidents-v6.0","node":"HMBpWxhwTAuu07-3Uk9vTA","reason":{"type":"script\_exception","reason":"runtime error","script\_stack":["return doc['incident\_update\_timestamp'].value - doc['wit\_cell\_16\_Value'].value;"," ^---- HERE"],"script":"return doc['incident\_update\_timestamp'].value - doc['wit\_cell\_16\_Value'].value;","lang":"painless","caused\_by":{"type":"class\_cast\_exception","reason":"Cannot apply operation to types [org.joda.time.MutableDateTime] and [org.joda.time.MutableDateTime]."}}}]},"status":500}  
at [https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229689:36](https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229689:36)  
at Function.Promise.try ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94576:20](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94576:20))  
at [https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94545:25](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94545:25)  
at Array.map ()  
at Function.Promise.map ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94544:28](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:94544:28))  
at callResponseHandlers ([https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229657:20](https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229657:20))  
at [https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229179:14](https://cicggncyberir.cicindia.in/bundles/kibana.bundle.js?v=8467:229179:14)  
at processQueue ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57541:37](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57541:37))  
at [https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57585:27](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:57585:27)  
at Scope.$digest ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:58723:15](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:58723:15))  
at Scope.$apply ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:59021:24](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:59021:24))  
at done ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53042:47](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53042:47))  
at completeRequest ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53268:7](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53268:7))  
at XMLHttpRequest.requestLoaded ([https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53196:9](https://cicggncyberir.cicindia.in/bundles/commons.bundle.js?v=8467:53196:9))

Can someone help me with this...

I used this [post](https://discuss.elastic.co/t/kibana-getting-todays-date/85766) as reference

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [March 13, 2020, 8:28am UTC](https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461/2 "2020-03-13T08:28:04Z")

</div>

It sounds like at least one of your fields is a `date`, not a number. If a field shows up as date type in your mapping, you have to access the value with `doc['myFieldNameHere'].value.millis` to get the timestamp value as a number.

Also see here [Kibana painless- Difference in dates in days](https://discuss.elastic.co/t/kibana-painless-difference-in-dates-in-days/169201)

---

<div class="post-metadata">

**Author:** ![Purushoth\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/purushoth_kumar/32/54579_2.png) [@Purushoth\_Kumar](https://discuss.elastic.co/u/Purushoth_Kumar)\
**Post date:** [March 13, 2020, 8:51am UTC](https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461/3 "2020-03-13T08:51:04Z")

</div>

> [@flash1293](#):
>
> .millis

Thanks alot @flash1293...I could able to get the desired outcome

---

<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:** [April 10, 2020, 8:51am UTC](https://discuss.elastic.co/t/painless-script-to-get-date-difference/223461/4 "2020-04-10T08:51:05Z")

</div>

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