# Calculate Difference between 2 Dates in Scipted Field

**URL:** <https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087>\
**Category:** Kibana\
**Created:** [August 15, 2020, 9:26am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087 "2020-08-15T09:26:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [August 15, 2020, 9:26am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/1 "2020-08-15T09:26:36Z")

</div>

Hi ,  
i try to create a new scripted field by calculating the difference(in minitues) between 2 dates in the following format :  
bookingStatus.confirmed :Jan 22, 2020 @ 12:20:00.000   
bookingStatus.wating :Jan 20, 2020 @ 10:20:00.000

i like the result be like : Xdays ,Xhours, Xminutes  
in this case duration = 2 days , 2 hours ,0minutes

i try this expression but i don't get the correct result:

doc['bookingStatus.confirmed'].value.getMillis() - doc['bookingStatus.wating'].value.getMillis()

 ![C1](https://us1.discourse-cdn.com/elastic/original/3X/4/e/4ed2110b674725d9ef6a2e7c235509b7bd8e68ae.png) ![C2](https://us1.discourse-cdn.com/elastic/original/3X/0/9/0972f025a1230c82ea8a2523058be558c90a2d80.png)

any help please !

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [August 18, 2020, 8:09am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/2 "2020-08-18T08:09:33Z")

</div>

In your example, you have the following dates:

```auto
bookingStatus.confirmed :Jan 22, 2020 @ 12:20:00.000
bookingStatus.wating :Jan 20, 2020 @ 10:20:00.000

```

The difference between those is roughly two days (20th of January to 22nd of January) - the "human readable" format will do some rounding to get a nice representation. It's currently not possible to get an exact duration in a human readable form, to replicate something like this you could try to implement it yourself in the scripted field and return a string instead.

---

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [August 18, 2020, 8:55am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/3 "2020-08-18T08:55:13Z")

</div>

Hi @flash1293 ,

i try with the to methodes:

 ![sdf1](https://us1.discourse-cdn.com/elastic/original/3X/c/0/c0da82cb98b9c2ee787a00a0526bd9e22f69a5ac.png)

it's possible to display "statusDate" ( scriptedfield :String ) in a visualization ( X-axe) ?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [August 18, 2020, 9:01am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/4 "2020-08-18T09:01:14Z")

</div>

It's possible to use the scripted field on an x axis, but not as a histogram, but as an ordinal "terms" scale. So the distance between two `statusDate`s on the axis won't be in scale with the duration

---

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [August 18, 2020, 9:11am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/5 "2020-08-18T09:11:16Z")

</div>

can you give me an example please !

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [August 18, 2020, 9:12am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/6 "2020-08-18T09:12:41Z")

</div>

- Go to Visualize
- Click Create new Visualization
- Select bar chart
- Add bucket
- Select "Terms" aggregation
- Select "statusDate" field
- Apply

---

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [August 18, 2020, 9:17am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/7 "2020-08-18T09:17:33Z")

</div>

I made an error in the first response , i want to display it in (Y-axis)

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [August 18, 2020, 9:19am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/8 "2020-08-18T09:19:18Z")

</div>

That's not possible with a string field, you will need to use your original number scripted field with a duration field formatter. The exact value you unfortunately can't show this way.

---

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [August 18, 2020, 9:23am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/9 "2020-08-18T09:23:51Z")

</div>

okey ,  
thank you for reply

---

<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:** [September 15, 2020, 9:23am UTC](https://discuss.elastic.co/t/calculate-difference-between-2-dates-in-scipted-field/245087/10 "2020-09-15T09:23:52Z")

</div>

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