# How do you use script fields with datetime subtraction

**URL:** <https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360>\
**Category:** Kibana\
**Created:** [January 15, 2016, 9:06pm UTC](https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360 "2016-01-15T21:06:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gsaray101](https://avatars.discourse-cdn.com/v4/letter/g/df788c/32.png) [@gsaray101](https://discuss.elastic.co/u/gsaray101)\
**Post date:** [January 15, 2016, 9:06pm UTC](https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360/1 "2016-01-15T21:06:27Z")

</div>

I am trying to use scripted fields to subtrack two datetime fields to determine how long it took for a file to processed.

doc.['time\_var1'].value - doc.['time\_var2'].value

doc being the index name.

I get this error:

Visualize: Request to Elasticsearch failed: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[rtl\_z8lBRrGHGQPtgehi2w]

my time\_var1 is something like this: 2016-01-13 16:34:59 the same format for the time\_var2.

Is there something am I missing?

---

<div class="post-metadata">

**Author:** ![gsaray101](https://avatars.discourse-cdn.com/v4/letter/g/df788c/32.png) [@gsaray101](https://discuss.elastic.co/u/gsaray101)\
**Post date:** [January 15, 2016, 9:33pm UTC](https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360/2 "2016-01-15T21:33:05Z")

</div>

If I could convert the time\_var1 to epoch before doing the math, that would work. But How do I convert date field to epoch in scripted field?

---

<div class="post-metadata">

**Author:** ![stormpython](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stormpython/32/7190_2.png) [@stormpython](https://discuss.elastic.co/u/stormpython)\
**Post date:** [January 16, 2016, 12:08am UTC](https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360/3 "2016-01-16T00:08:59Z")

</div>

gsaray101, I am not sure if you can convert to epoch, but you can convert to other metrics, like day, hour, min, seconds, etc.

Take a look at the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_lucene_expressions_scripts).

---

<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 6, 2017, 2:04pm UTC](https://discuss.elastic.co/t/how-do-you-use-script-fields-with-datetime-subtraction/39360/4 "2017-07-06T14:04:51Z")

</div>


