# Subtraction between current date and doc created date

**URL:** <https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405>\
**Category:** Kibana\
**Created:** [February 24, 2023, 6:30am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405 "2023-02-24T06:30:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PappuSingh](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@PappuSingh](https://discuss.elastic.co/u/PappuSingh)\
**Post date:** [February 24, 2023, 6:30am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/1 "2023-02-24T06:30:06Z")

</div>

Hi,  
Please see the below code sample.

long l1=(new Date().getTime());  
//emit(l1);  
long l= ChronoUnit.SECONDS.between(l1, doc['timestamp'].value);  
if(l\<=100000){  
emit('New')  
}  
else{emit('Old')}

I am getting the below Error

Cannot cast from [long] to [java.time.temporal.Temporal]. Verify that you have correctly set the runtime field type.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 24, 2023, 7:00am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/2 "2023-02-24T07:00:18Z")

</div>

Hi @PappuSingh Welcome to the community.

Perhaps this will help it is nearly the same use case.

> [@Calculate elapsed time in Kibana?](https://discuss.elastic.co/t/calculate-elapsed-time-in-kibana/316575/2):
>
> Hi @TXBigDawg1836 Welcome to the community Yup add a runtime field to the Data View here is the code long datenow = new Date().getTime(); long datewas = doc['@timestamp'].value.getMillis(); emit (datenow - datewas); Create / Set the Field Chose your format.... In Discover Now Table...

---

<div class="post-metadata">

**Author:** ![PappuSingh](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@PappuSingh](https://discuss.elastic.co/u/PappuSingh)\
**Post date:** [February 24, 2023, 11:17am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/3 "2023-02-24T11:17:24Z")

</div>

How I can calculate hours or day?

Please see my code below  
long datenow = new Date().getTime();

long datewas = doc['createdTime'].value.getMillis();

long milisecondnd=datenow - datewas;

long second=milisecondnd/1000;

long hours=second/3600;

long daycount=hours/24;

emit(daycount);

the wrong Number is displaying it is showing 1

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/7/6711f4dbe6d38f16d6a72532c0c07edef0da3dd2.png)

CreatedTime="Jan 21, 2023 @ 01:29:06.500"

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 24, 2023, 3:22pm UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/4 "2023-02-24T15:22:24Z")

</div>

Do you want Days in Integer or Float?

Field Type Double

```auto
long datenow = new Date().getTime();
long datewas = doc['@timestamp'].value.getMillis();
double days = (datenow - datewas) / 86400000.0;
emit (days);

```

Field Type Long

```auto
long datenow = new Date().getTime();
long datewas = doc['@timestamp'].value.getMillis();
long days = (datenow - datewas) / 86400000;
emit (days);

```

---

<div class="post-metadata">

**Author:** ![PappuSingh](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@PappuSingh](https://discuss.elastic.co/u/PappuSingh)\
**Post date:** [March 1, 2023, 10:54am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/5 "2023-03-01T10:54:44Z")

</div>

Thank you it is working now.

---

<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:** [March 29, 2023, 10:55am UTC](https://discuss.elastic.co/t/subtraction-between-current-date-and-doc-created-date/326405/6 "2023-03-29T10:55:05Z")

</div>

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