# Painless Script to calculate time difference between 2 dates

**URL:** <https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 16, 2022, 3:57am UTC](https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335 "2022-06-16T03:57:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avnish\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avnish_singh/32/71923_2.png) [@Avnish\_Singh](https://discuss.elastic.co/u/Avnish_Singh)\
**Post date:** [June 16, 2022, 3:57am UTC](https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335/1 "2022-06-16T03:57:57Z")

</div>

Hi I have 2 columns as date datatype in my Kibana Index Pattern and I want to add a third column as the difference between the 2 columns:  
Here is sample and format of the columns:

```auto
Col1: Jun 16, 2022 , 09:00:00.000
Col2: Jun 16, 2022 , 09:10:07.999

```

Now I want to have a third column as TimeDifference in the following format:

```auto
TimeDiff: 00:10:07.999

```

Can anyone help me with the script please I tried a few but everytime it complains about invalid script, here is the script that I tried and it failed:

```auto
def diffInMs = doc['col2'].value.getMillis() - doc['col1'].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;

```

It would be really helpful if anyone could point out what did I miss out in this script.  
Thanks

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 17, 2022, 10:46pm UTC](https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335/2 "2022-06-17T22:46:38Z")

</div>

Hi @Avnish_Singh .

I don't know if it's exactly like your example but with your script code I got the desired output.

```auto
POST teste/_doc
{
  "col1": "2022-06-16T09:00:00.000",
  "col2": "2022-06-16T09:10:07.999"
}

GET teste/_search
{
  "script_fields": {
    "time_diff": {
      "script": {
        "source": """
          def diffInMs = doc['col2'].value.getMillis() - doc['col1'].value.getMillis();
          return new SimpleDateFormat('HH:mm:ss.SSS').format(diffInMs);
      """
      }
    }
  }
}

```

---

<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:** [July 15, 2022, 10:47pm UTC](https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335/3 "2022-07-15T22:47:04Z")

</div>

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