# How to find the difference between current time and firsteventdetected time using scripting

**URL:** <https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895>\
**Category:** Elasticsearch\
**Created:** [January 19, 2019, 1:25pm UTC](https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895 "2019-01-19T13:25:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)\
**Post date:** [January 19, 2019, 1:25pm UTC](https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895/1 "2019-01-19T13:25:12Z")

</div>

Hi All,

I am trying to find the difference between the current time and firsteventdetected time from the index and below is the query,

```
POST transaction*/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "age": {
      "script": {
        "lang": "painless",
        "source": "(doc['FirstEventDetectedTime'].value - new Date().getTime() * 1000)"
      }
    }
  }
}

```

Below is the error I am getting,

```
{
  "error": {
    "root_cause": [
      {
        "type": "class_cast_exception",
        "reason": "class_cast_exception: Cannot apply [-] operation to types [org.joda.time.MutableDateTime] and [java.lang.Long]."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "transaction",
        "node": "77dK4fb_Qw26iXZGPYy-Gg",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "doc['FirstEventDetectedTime'].value - new Date().getTime() * 1000",
            " ^---- HERE"
          ],
          "script": "doc['FirstEventDetectedTime'].value - new Date().getTime() * 1000",
          "lang": "painless",
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class_cast_exception: Cannot apply [-] operation to types [org.joda.time.MutableDateTime] and [java.lang.Long]."
          }
        }
      }
    ],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "class_cast_exception: Cannot apply [-] operation to types [org.joda.time.MutableDateTime] and [java.lang.Long]."
    }
  },
  "status": 500
}

```

The data in the **FirstEventDetectedTime** field has like this **"2019-01-10T12:55:30.000Z"**

Below is the query I tried to get the current time,

```
POST transaction*/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "age": {
      "script": {
        "lang": "painless",
        "source": "new Date().getTime() * 1000"
      }
    }
  }
}

```

above Query response,

> {  
> "\_index": "transaction",  
> "\_type": "1",  
> "\_id": "QrhrZWgBaqAekAe-SdEr",  
> "\_score": 1,  
> "fields": {  
> "age": [  
> 1547903795164000  
> ]  
> }  
> }

Please let me know your thoughts on finding the difference between the current time and firsteventdetected time.

Regards,  
Ganeshbabu R

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [January 21, 2019, 3:14pm UTC](https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895/2 "2019-01-21T15:14:31Z")

</div>

To get the difference between the two times in milliseconds you can use this Painless script:

```auto
doc['FirstEventDetectedTime'].value.getMillis() - System.currentTimeMillis()

```

---

<div class="post-metadata">

**Author:** ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)\
**Post date:** [January 23, 2019, 9:25am UTC](https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895/3 "2019-01-23T09:25:50Z")

</div>

Thanks @abdon for your inputs and it's worked rightly.

---

<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:** [February 20, 2019, 9:25am UTC](https://discuss.elastic.co/t/how-to-find-the-difference-between-current-time-and-firsteventdetected-time-using-scripting/164895/4 "2019-02-20T09:25:53Z")

</div>

Closed on February 20, 2019, 9:25am UTC

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