"diff date" and "if"

Hello! I am trying to make a classifier where I need to see the difference of dates and determine if they are on time. I am trying to do this script but it shows error in the index, any recommendations?

def diff = doc['tranDate_as_date'].value - doc['businessDate_as_date'].value;
def hrsdiff=diff/1000/60/60;

if (hrsdiff >23.5){
return "Delay"
}else{
return "On time"
}

I did this in the Painless Lab for testing. Just need to change input fields params.tranDate_as_date and params.businessDate_as_date to Scripted Fields doc['tranDate_as_date'].value and doc['businessDate_as_date'].value.

Parameters

{
  "tranDate_as_date": "2021-02-27T19:28:48+00:00",
  "businessDate_as_date": "2021-02-28T09:28:48+00:00"
}

Script

ZonedDateTime tranDate_as_date = ZonedDateTime.parse(params.tranDate_as_date); 
ZonedDateTime businessDate_as_date = ZonedDateTime.parse(params.businessDate_as_date); 

long diff = ChronoUnit.HOURS.between(tranDate_as_date, businessDate_as_date);

if (diff > 23.5) {
    return "Delay";
} else {
    return "On time";
}

Datetime functions in Painless Documentation

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