# Calculate the timedifference between current time and future time

**URL:** <https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274>\
**Category:** Elasticsearch\
**Created:** [January 23, 2020, 2:55pm UTC](https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274 "2020-01-23T14:55:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandu5565](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@chandu5565](https://discuss.elastic.co/u/chandu5565)\
**Post date:** [January 23, 2020, 2:55pm UTC](https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274/1 "2020-01-23T14:55:52Z")

</div>

Hi,

I have data in the mongo db like below.  
"last-date": "2020-01-30T07:30:00Z"  
"type": "certificate"  
"daysleft": "7"

I had inserted this kind of records in to elasticsearch. I wanted to daily monitor the number of **daysleft** for the certificate after differentiating with current date and update the same record which is existing in elasticsearch.

my expectations are like below on the below dates it should dispaly the differece by calculating lastdate - Time.now(). Every second or every day it has to be updated once

2020-01-24 it should show 6  
2020-01-25 it should show 5  
2020-01-26 it should show 4  
2020-01-27 it should show 3  
2020-01-28 it should show 2  
2020-01-24 it should show 1  
2020-01-30 it should show 0

Is it possible to do in kibana or elasticsearch. If yes how can I achieve it. Help is really appreciated.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 23, 2020, 3:45pm UTC](https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274/2 "2020-01-23T15:45:21Z")

</div>

Hey,

I may be missing some context here, but wouldn't it be easier, if you adapted your query by searching for documents with a certain last\_date using the [range query](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-range-query.html#ranges-on-dates). This way there would be no need for updating that field, and you could probably simply omit it.

---

<div class="post-metadata">

**Author:** ![chandu5565](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@chandu5565](https://discuss.elastic.co/u/chandu5565)\
**Post date:** [January 23, 2020, 4:24pm UTC](https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274/3 "2020-01-23T16:24:27Z")

</div>

Thanks for the reply i will explain you my scenario.

I have documents in mongo db which has **created\_from** and **created\_to** both are timestamps.  
I have to calculate the time difference between these two fields and display it as **validity**.  
Then I have to keep track of the validity till it get expires. for example if validity between two "created\_from and created to is 10 days"  
while indexing data to elasticsearch i will have these three fields **created\_from and created\_to and validity**.  
Then I need to find Number of days left for expiry i.e field "time\_left" is **validity - time.now()**. I have to perform this step daily so I updated it as time.now().  
Then I have to display the count of records which are falling under which range like \> or \< or \<=.

I wanted to calculate with fields so how can I do it. created\_to will be the future date which will be 30 or 90 days greater than today

---

<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:** [February 20, 2020, 4:24pm UTC](https://discuss.elastic.co/t/calculate-the-timedifference-between-current-time-and-future-time/216274/4 "2020-02-20T16:24:32Z")

</div>

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