Substracting two datetime fields


This is a field in my document:

"date_found": "2018-04-23T22:17:46.6387919+03:00"

My goal is to calculate some value from difference between two datetime fields (e.g. dt1 - dt2)

I running a script to perform such operation:

updateDescriptor.Script(a => a.Inline("ctx._source.lifetime = ctx._source.date_deleted - ctx._source.date_found"));

This operation results with the following error:

"Cannot apply [-] operation to types [java.lang.String] and [java.lang.String]."

How it is advised to perform operations on datetime fields?

(David Pilato) #2

I don't remember by heart the syntax but I believe you need first to transform the date object to ms since epoch and then you can do a substraction.

Have a look at what are the available methods for a date type field.


Can I perform such conversion as part of script execution? Where I can find list of available methods you are mentioning?

(R_C) #4

You can refer to the below link -

(David Pilato) #5

On a mobile now but a quick search and I found



On mobile now but from quick view it looks like what I need.

Thanks man

(system) #7

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