# 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:** 1\
**Showing post:** 2

<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); """ } } …

---

_[View the full topic](https://discuss.elastic.co/t/painless-script-to-find-the-difference-between-two-timestamp-values/329829)._
