elasticsearchkibana-5elasticsearch-painless

Calculate two date duration in Elastic 6.7 using painless script


I used below simple expression for getting duration:

doc['endTime'].date.millisOfDay - doc['startTime'].date.millisOfDay

But the problem starts when, endTime crosses the startTime day.

Example: If startTime is 23:50 and endTime for the same is 00:12, we crossed by midnight, which changes the date as well.

In that way I am getting absolutely wrong duration, except all the scenarios when both time lies with in the same day result is as expected.

Help on how exactly i can make this.


Solution

  • You should simply subtract the absolute milliseconds value since the epoch (instead of milliseconds since the start of the day):

    doc['endTime'].date.millis - doc['startTime'].date.millis