# Problems with date subtraction

**URL:** <https://discuss.elastic.co/t/problems-with-date-subtraction/87123>\
**Category:** Kibana\
**Created:** [May 25, 2017, 12:35pm UTC](https://discuss.elastic.co/t/problems-with-date-subtraction/87123 "2017-05-25T12:35:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mickael\_fraga](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mickael\_fraga](https://discuss.elastic.co/u/Mickael_fraga)\
**Post date:** [May 25, 2017, 12:35pm UTC](https://discuss.elastic.co/t/problems-with-date-subtraction/87123/1 "2017-05-25T12:35:20Z")

</div>

Good morning, I would like help.

My problem is this, I need to create a field in kibana which is the subtraction of two date fields ex (2017-01-01 - 2017-01-11) where the result would be 10 days

Script  
I'm creating a painless doc ['date1']. Value - doc ['date2']. Value is not working  
help me

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [May 26, 2017, 9:23pm UTC](https://discuss.elastic.co/t/problems-with-date-subtraction/87123/2 "2017-05-26T21:23:25Z")

</div>

Are your field strings or date types?

You can look at the index pattern to see. For example my date1 and date2 are dates (they're also scriptable and aggregatable which I think they need to be if you use them in a scripted field);

 ![](https://us1.discourse-cdn.com/elastic/original/3X/0/8/08b01307e5e6b25b1ec12db59b58e1e3a60b8f29.png)

Then I created this scripted field (the difference in dates is in milliseconds so I divide by 1000 to get seconds, by 60 to get minutes, by 60 to get hours, by 24 to get days);  
`(doc['date2'].value - doc['date1'].value) / 1000 / 60/ 60 /24`

 ![](https://us1.discourse-cdn.com/elastic/original/3X/1/4/1413499023dd4770f9094548baab17f8b167f55b.png)

And the result is 10;

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

If you'r dates are strings, I'll have to find the trick to convert them and then I expect it would be the same formula.

Regards,  
Lee

---

<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:** [June 23, 2017, 9:23pm UTC](https://discuss.elastic.co/t/problems-with-date-subtraction/87123/3 "2017-06-23T21:23:50Z")

</div>

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