# How to calculate date diffs using ingest node

**URL:** https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652
**Category:** Elasticsearch
**Created:** [February 23, 2019, 7:37am UTC](https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652 "2019-02-23T07:37:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Christian\_SANCHEZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_sanchez/32/43557_2.png) [@Christian\_SANCHEZ](https://discuss.elastic.co/u/Christian_SANCHEZ)
#### Post date: [February 23, 2019, 7:37am UTC](https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652/1 "2019-02-23T07:37:02Z")

</div>

Hi  
I have a basic date diff between 2 dates to do and display it as weeks.  
If i use the Kibana scripted fields, it works fine using the following syntax :

Math.round((doc['last\_detected'].value.toInstant().toEpochMilli() - doc['first\_detected'].value.toInstant().toEpochMilli())/((3600000\*24))/7)

However, I would like to have it calculated during the document ingestion but i can not figure which is the syntax ?

Any guess ?

---

<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: [February 24, 2019, 1:02pm UTC](https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652/2 "2019-02-24T13:02:58Z")

</div>

The `doc` syntax to access field only works at search or aggregation time. To access fields in an ingest processor, you can use the `ctx["field_name"]` syntax.

Something like the following processor should work:

```auto
    {
      "script": {
        "source": """
  if(ctx.containsKey("first_detected") && ctx.containsKey("last_detected")) {
    ZonedDateTime first = ZonedDateTime.parse(ctx["first_detected"]);
    ZonedDateTime last = ZonedDateTime.parse(ctx["last_detected"]);
    ctx.diff = ChronoUnit.MILLIS.between(first, last);
  }
"""
      }
    }

```

---

<div class="post-metadata">

### Author: ![Christian\_SANCHEZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_sanchez/32/43557_2.png) [@Christian\_SANCHEZ](https://discuss.elastic.co/u/Christian_SANCHEZ)
#### Post date: [February 26, 2019, 9:49am UTC](https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652/3 "2019-02-26T09:49:27Z")

</div>

Hi  
Thanks a lot.  
Regards

---

<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: [March 26, 2019, 9:49am UTC](https://discuss.elastic.co/t/how-to-calculate-date-diffs-using-ingest-node/169652/4 "2019-03-26T09:49:31Z")

</div>

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