# Create scripted field showing time differences

**URL:** https://discuss.elastic.co/t/create-scripted-field-showing-time-differences/275134
**Category:** Elasticsearch
**Tags:** painless
**Created:** [June 7, 2021, 10:36am UTC](https://discuss.elastic.co/t/create-scripted-field-showing-time-differences/275134 "2021-06-07T10:36:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Elaak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elaak/32/55014_2.png) [@Elaak](https://discuss.elastic.co/u/Elaak)
#### Post date: [June 7, 2021, 10:36am UTC](https://discuss.elastic.co/t/create-scripted-field-showing-time-differences/275134/1 "2021-06-07T10:36:02Z")

</div>

Hi everyone,

I am running Elastic Stack 7.9 with a basic license. In my data I have two date fields and I want to create a scripted field that writes the difference in a nice format. I easily get the difference by just substracting:

```
diff = doc['date_updated'].value.getMillis() - doc['date_create'].value.getMillis()

```

But I am trying to find a way to present the difference in a nice simple way, for example `1 day 3 hours`. As I understand the documentation, formatting of date fields is done using `momentjs`, but how can I access it `painless`, and especially the `diff` method?

I have tried things such as (for simplicity ´date1, date2´ are the two target date field values):

```
var date1 = moment(doc['date_updated'].value.getMillis())

```

but it already here fails as it does not know `moment`.

What am I doing wrong, or is there any other obvious way to create my wanted scripted field?

Many thanks for any help!

---

<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 5, 2021, 10:36am UTC](https://discuss.elastic.co/t/create-scripted-field-showing-time-differences/275134/2 "2021-07-05T10:36:56Z")

</div>

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