# How can I define a runtime field which shows the time elapsed since the value of a date field

**URL:** <https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [September 28, 2021, 10:47am UTC](https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339 "2021-09-28T10:47:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsouth](https://avatars.discourse-cdn.com/v4/letter/n/ecccb3/32.png) [@nsouth](https://discuss.elastic.co/u/nsouth)\
**Post date:** [September 28, 2021, 10:47am UTC](https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339/1 "2021-09-28T10:47:29Z")

</div>

I have an index and I want a field called `durationv2`. If the field `command_details.endTime` exists, I want `durationv2` to be the difference between `command_details.startTime` and `command_details.endTime`. Otherwise, I want it to be the difference between `command_details.startTime` and `now`. The goal is for a live viewer to either see how long a command _took_ or how long it _is taking_. Would something like the following work?

Right now I'm getting a _resource\_already\_exists\_exception_ error on the index. I'm guessing that I'm not using the right syntax to add a new mapping to my index.

Also, it feels like it would be more efficient to calculate the current time once and to pass it as a parameter, but I'm not sure how to do that.

```auto
PUT transform-my-test-7/
{
  "mappings": {
    "runtime":{
      "durationv2": {
        "type": "long",
        "script": {
          "source": "emit(doc.['command_details.endTime'].empty ? Instant.ofEpochMilli(new Date()).getMillis() - doc['command_details.startTime'].getMillis() : doc['command_details.endTime'].getMillis() - doc['command_details.startTime'].getMillis()) "
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![nsouth](https://avatars.discourse-cdn.com/v4/letter/n/ecccb3/32.png) [@nsouth](https://discuss.elastic.co/u/nsouth)\
**Post date:** [September 28, 2021, 1:12pm UTC](https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339/2 "2021-09-28T13:12:41Z")

</div>

I think I figured it out. First, when adding a mapping, I needed to use the `POST` verb instead of `PUT`. Here's what I ended with. I'll need to circle back and account for time zones in the future, but this serves my needs for now.

```auto
PUT transform-my-test-7/_mapping
{
  "runtime":{
    "durationv2": {
      "type": "long",
      "script": {
        "source": """
          if (!doc['command_details.endTime'].empty && !doc['command_details.startTime'].empty ) {
            emit(doc['command_details.endTime'].value.toInstant().toEpochMilli() - doc['command_details.startTime'].value.toInstant().toEpochMilli())
          }
          else if (!doc['command_details.startTime'].empty) {
            emit(System.currentTimeMillis() - doc['command_details.startTime'].value.toInstant().toEpochMilli())
          }
        """
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [September 28, 2021, 2:35pm UTC](https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339/3 "2021-09-28T14:35:59Z")

</div>

Hi @nsouth, thanks for posting your solution.

> I'll need to circle back and account for time zones in the future, but this serves my needs for now.

Check out the help doc [Using Datetime in Painless](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html).

The strategy will be:

1. Create a `ZonedDateTime` from an `Instant`, as seen in the example on that page:

```auto
long milliSinceEpoch = 434931330000L;
Instant instant = Instant.ofEpochMilli(milliSinceEpoch);
ZonedDateTime zdt1 = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));

```

1. Then use `ChronoUnit.MILLIS.between(zdt1, zdt2)`

```auto
long differenceInMillis = ChronoUnit.MILLIS.between(zdt1, zdt2);

```

---

<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 26, 2021, 2:36pm UTC](https://discuss.elastic.co/t/how-can-i-define-a-runtime-field-which-shows-the-time-elapsed-since-the-value-of-a-date-field/285339/4 "2021-10-26T14:36:28Z")

</div>

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