# Datefields time difference in scripted field produce zero always

**URL:** <https://discuss.elastic.co/t/datefields-time-difference-in-scripted-field-produce-zero-always/165827>\
**Category:** Kibana\
**Created:** [January 25, 2019, 8:32pm UTC](https://discuss.elastic.co/t/datefields-time-difference-in-scripted-field-produce-zero-always/165827 "2019-01-25T20:32:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranamesh](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Pranamesh](https://discuss.elastic.co/u/Pranamesh)\
**Post date:** [January 25, 2019, 8:32pm UTC](https://discuss.elastic.co/t/datefields-time-difference-in-scripted-field-produce-zero-always/165827/1 "2019-01-25T20:32:09Z")

</div>

Hi,

I want to find out the difference between two date fields using scripted value. However, the result is always coming as zero. I am using painless as the language (tried with expression also, but still result is zero). I am using the following code in scripted field

`doc['time1'].value.getMillis() - doc['time2'].value.getMillis()`

Both 'time1' and 'time2' are date fields as shown in Discover tab.

---

<div class="post-metadata">

**Author:** ![DrPsyche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drpsyche/32/36918_2.png) [@DrPsyche](https://discuss.elastic.co/u/DrPsyche)\
**Post date:** [January 31, 2019, 3:28pm UTC](https://discuss.elastic.co/t/datefields-time-difference-in-scripted-field-produce-zero-always/165827/2 "2019-01-31T15:28:07Z")

</div>

Hi Pranamesh,

In case you need to do date math, you will have to cast the field values to date types in Painless. Check some examples in the article:  
[Elastic: Using Painless in Kibana scripted fields By Tanya Bragin on Elastic](https://www.elastic.co/blog/using-painless-kibana-scripted-fields) [web site](https://theessayservice.org/)

Steve

---

<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, 3:35pm UTC](https://discuss.elastic.co/t/datefields-time-difference-in-scripted-field-produce-zero-always/165827/3 "2019-02-28T15:35:04Z")

</div>

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