Subtraction between current date and doc created date

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.

Hi @PappuSingh Welcome to the community.

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

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

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

Do you want Days in Integer or Float?

Field Type Double

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

Field Type Long

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

Thank you it is working now.

1 Like

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