Painless Script to calculate time difference between 2 dates

Hi @Avnish_Singh .

I don't know if it's exactly like your example but with your script code I got the desired output.

POST teste/_doc
{
  "col1": "2022-06-16T09:00:00.000",
  "col2": "2022-06-16T09:10:07.999"
}

GET teste/_search
{
  "script_fields": {
    "time_diff": {
      "script": {
        "source": """
          def diffInMs = doc['col2'].value.getMillis() - doc['col1'].value.getMillis();
          return new SimpleDateFormat('HH:mm:ss.SSS').format(diffInMs);
      """
      }
    }
  }
}
1 Like