# Painless script to find the difference between two timestamp values

**URL:** <https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 12, 2023, 11:42am UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829 "2023-04-12T11:42:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvadiga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rvadiga/32/102601_2.png) [@rvadiga](https://discuss.elastic.co/u/rvadiga)\
**Post date:** [April 12, 2023, 11:42am UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829/1 "2023-04-12T11:42:53Z")

</div>

Hi All,  
I am trying to define a scripted field in ES 7.17 scope where the difference between two timestamp field values need to be defined.  
I did looked into the painless documentation, but could not exactly find some matching reference.  
Could someone please share any sample painless script to find the difference between two timestamp field values in minutes?

Thanks  
Venkatesh

---

<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:** [April 12, 2023, 12:09pm UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829/2 "2023-04-12T12:09:08Z")

</div>

Hi @rvadiga

Maybe this answer help you to start:

> [@Painless Script to calculate time difference between 2 dates](https://discuss.elastic.co/t/painless-script-to-calculate-time-difference-between-2-dates/307335/2):
>
> Hi @Avnish_Singh . I don't know if it's exactly like your example but with your script code I got the desired output. 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:** ![rvadiga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rvadiga/32/102601_2.png) [@rvadiga](https://discuss.elastic.co/u/rvadiga)\
**Post date:** [April 12, 2023, 12:14pm UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829/3 "2023-04-12T12:14:13Z")

</div>

I tried with the below option

{  
"script": {  
"script": {  
"inline": "doc['col1'].value.toInstant().toEpochMilli() - doc['col2'].value.toInstant().toEpochMilli() \> params.difference",  
"lang": "painless",  
"params": {  
"difference": 60000  
}  
}  
}  
}

Do you suggest whether it is correct definition to find minute level difference.

Regards  
Venkatesh

---

<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:** [April 12, 2023, 12:31pm UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829/4 "2023-04-12T12:31:31Z")

</div>

Please use the format tools code for help visualize the script.

Do you want something like this:

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

POST teste/_doc
{
  "col1": "2022-06-16T09:00:00.000",
  "col2": "2022-06-16T0:11:00.00"
}

GET teste/_search
{
  "_source": [
    "*"
  ],
  "script_fields": {
    "time_diff": {
      "script": {
        "params": {
          "difference": 10
        },
        "source": """
          def diffInMs = doc['col2'].value.getMillis() - doc['col1'].value.getMillis();
          def minutes = (diffInMs / 1000) / 60;
          return minutes > params.difference;
      """
      }
    }
  }
}

```

---

<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:** [May 10, 2023, 12:31pm UTC](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829/5 "2023-05-10T12:31:44Z")

</div>

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