# How to calculate timestamp difference using Kibana scripted field feature?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-timestamp-difference-using-kibana-scripted-field-feature/248133>\
**Category:** Kibana\
**Tags:** kql-kibana-query-language\
**Created:** [September 10, 2020, 8:44am UTC](https://discuss.elastic.co/t/how-to-calculate-timestamp-difference-using-kibana-scripted-field-feature/248133 "2020-09-10T08:44:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Chougule](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahul_chougule/32/71789_2.png) [@Rahul\_Chougule](https://discuss.elastic.co/u/Rahul_Chougule)\
**Post date:** [September 10, 2020, 8:44am UTC](https://discuss.elastic.co/t/how-to-calculate-timestamp-difference-using-kibana-scripted-field-feature/248133/1 "2020-09-10T08:44:37Z")

</div>

I have two date fields in my Elasticsearch index: 1. Submit\_Date & 2. Completed\_Date.  
I want to add new field which will be diff between Completed\_Date and Submit\_Date...  
but if Completed\_Date is empty I want the diff between current system timestamp and Submit\_Date...

I have written following code but getting error:

if(doc['Completed\_Date'] && doc['Submit\_Date']) {  
return doc['Completed\_Date'] - doc['Submit\_Date']  
} else {  
return new Date().getTime() - doc['Submit\_Date']  
}

please help..what wrong thing I'm doing here...

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [September 10, 2020, 9:09am UTC](https://discuss.elastic.co/t/how-to-calculate-timestamp-difference-using-kibana-scripted-field-feature/248133/2 "2020-09-10T09:09:06Z")

</div>

Here is an example

```auto
GET my-index/_search
{
  "_source": ["Submit_Date", "Completed_Date"],  
  "script_fields": {
    "daysBetween": {
      "script": {
        "source": "(doc['Completed_Date'].value.toInstant().toEpochMilli() - doc['Submit_Date'].value.toInstant().toEpochMilli()) / params.factor",
        "params": {
          "factor": 86400000
        }
      }
    }
  }
}

```

Computing data on the fly with scripted fields can be very resource intensive and can have a direct impact on Kibana performance. Keep in mind that there’s no built-in validation of a scripted field. If your scripts are buggy, you’ll get exceptions whenever you try to view the dynamically generated data.

Use something like this to check if field exist in the doc

`if (!doc.containsKey('myfield') || doc['myfield'].empty) { return "unavailable" } else { return doc['myfield'].value }`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 8, 2020, 9:09am UTC](https://discuss.elastic.co/t/how-to-calculate-timestamp-difference-using-kibana-scripted-field-feature/248133/3 "2020-10-08T09:09:21Z")

</div>

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