# Parse Date from Script\_Fields Params

**URL:** <https://discuss.elastic.co/t/parse-date-from-script-fields-params/303329>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 26, 2022, 8:36pm UTC](https://discuss.elastic.co/t/parse-date-from-script-fields-params/303329 "2022-04-26T20:36:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prashant\_zeta](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@prashant\_zeta](https://discuss.elastic.co/u/prashant_zeta)\
**Post date:** [April 26, 2022, 8:36pm UTC](https://discuss.elastic.co/t/parse-date-from-script-fields-params/303329/1 "2022-04-26T20:36:59Z")

</div>

I am trying to find the elapsed time  
**elapsed time** : duration of time b/w `now` and `createdTime`

To pass the current\_time (ie `now`) I have added it to the `params`, and I can access that in the `source` field by writing `params['now']`  
The problem is that this value of `params['now']` is a string and not of type date

The below example works as I have added `doc['updatedTime']`, in place of `params['now']`  
how can I get it to work with `params['now']`

WORKING

```auto
GET entity.incident_action_item/_search
{
  "script_fields": {
    "timeElapsed": {
      "script": {
        "source": "doc['updatedTime'].value.toInstant().getEpochSecond() - doc['createdTime'].value.toInstant().getEpochSecond()",
        "params": {
          "now": "2022-03-31T17:18:28.153+0530"
        }
      }
    }
  }
}

```

NOT WORKING

```auto
GET entity.incident_action_item/_search
{
  "script_fields": {
    "timeElapsed": {
      "script": {
        "source": "params['now'].value.toInstant().getEpochSecond() - doc['createdTime'].value.toInstant().getEpochSecond()",
        "params": {
          "now": "2022-03-31T17:18:28.153+0530"
        }
      }
    }
  }
}

```

I have tried multiple combinations and tried different methods supported by the painless langauge, I was not able to get it work

I faced one or the other exception

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 27, 2022, 3:53am UTC](https://discuss.elastic.co/t/parse-date-from-script-fields-params/303329/2 "2022-04-27T03:53:34Z")

</div>

This example below returns the difference of days between dates, you can edit to achieve your goal.  
The difference is that the currentdate is already in the millis.

```auto
GET index_002/_search
{
  "_source": [
    "name", "create_date"
  ],
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "diff_dates": {
      "script": {
        "source": """
        Instant instant = Instant.ofEpochMilli(params.current_date);
        ZonedDateTime now = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
        return doc['create_date'].value.until(now, ChronoUnit.DAYS);
        """,       
        "params": {
          "current_date": 1650516960000
        }
      }
    }
  }
}

```

---

<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:** [May 25, 2022, 3:54am UTC](https://discuss.elastic.co/t/parse-date-from-script-fields-params/303329/3 "2022-05-25T03:54:28Z")

</div>

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