# Need a script field tag to calculate time difference

**URL:** https://discuss.elastic.co/t/need-a-script-field-tag-to-calculate-time-difference/277438
**Category:** Elasticsearch
**Created:** [June 30, 2021, 9:37am UTC](https://discuss.elastic.co/t/need-a-script-field-tag-to-calculate-time-difference/277438 "2021-06-30T09:37:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vikram.sablok](https://avatars.discourse-cdn.com/v4/letter/v/3ec8ea/32.png) [@vikram.sablok](https://discuss.elastic.co/u/vikram.sablok)
#### Post date: [June 30, 2021, 9:37am UTC](https://discuss.elastic.co/t/need-a-script-field-tag-to-calculate-time-difference/277438/1 "2021-06-30T09:37:56Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/7/670201adc8a666a2e654a879b8ad2520ec23614e.png)  
-(minus)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/5/b5e994a3973e26292a6d1ec5f6ada154c9def506.png)

this will give me overall time taken for a request. I can use this field to show average time over a period of time.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [July 1, 2021, 2:27pm UTC](https://discuss.elastic.co/t/need-a-script-field-tag-to-calculate-time-difference/277438/2 "2021-07-01T14:27:05Z")

</div>

There are two solutions to this in addition to a script field - which I would both prefer

First, there is something called a [runtime field](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/runtime.html) similar to a script field, that allows you to configure a dynamic field in the mapping, that substracts the two dates via a script. The advantage of having this as a runtime field is, that you can treat this across all your queries as if this was an indexed field by just specifying a field name. And the advantage of this approach is, that...

you can go with the second approach and create this field on index time using a [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/script-processor.html). When you are doing that and you pick the same field name as with the runtime field in the beginning, you will not need to change your query, when you switch from a runtime field to the script processor.

Hope that helps!

---

<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: [July 29, 2021, 2:27pm UTC](https://discuss.elastic.co/t/need-a-script-field-tag-to-calculate-time-difference/277438/3 "2021-07-29T14:27:22Z")

</div>

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