# Access current time in kibana scripted field

**URL:** <https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897>\
**Category:** Kibana\
**Created:** [April 20, 2016, 10:29am UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897 "2016-04-20T10:29:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan1](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johan1](https://discuss.elastic.co/u/johan1)\
**Post date:** [April 20, 2016, 10:29am UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/1 "2016-04-20T10:29:33Z")

</div>

Hi,

We are trying to visualize some data based on a timespan between two dates date1 and date2. I'm trying to create a scripted field to return the amount of days between the two dates. This works as expected however date2 can in certain cases be in the future in which case we need to use the span between date1 and current time.

Expression I would like to run:  
doc['date2'].value \< now) ? doc['date2'] - doc['date1'] : now - doc['date1']

I cannot find any way to access current time in a scripted field expression. Is this possible?

We are running Kibana 4.5.0

Thanks!

---

<div class="post-metadata">

**Author:** ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)\
**Post date:** [April 20, 2016, 10:15pm UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/2 "2016-04-20T22:15:38Z")

</div>

> [@johan1](#):
>
> access current time in a scripted field expression

This isn't possible with Lucene, which is the default query language. Elasticsearch will be getting a query language in the future that should allow this, but that doesn't really help you right now...

Based on some other replies here, it sounds like you should be able to do this with groovy scripting, something you can do today, though it's a little trickier to use. See [the scripting docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html) to learn more.

---

<div class="post-metadata">

**Author:** ![johan1](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johan1](https://discuss.elastic.co/u/johan1)\
**Post date:** [April 21, 2016, 6:20am UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/3 "2016-04-21T06:20:06Z")

</div>

Hi Joe,

Thanks for your reply. I have tried the following but neither seem to work:

(new Date()).getTime()  
System.currentTimeMillis()

Maybe they are limited due to security?

---

<div class="post-metadata">

**Author:** ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)\
**Post date:** [April 21, 2016, 5:23pm UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/4 "2016-04-21T17:23:08Z")

</div>

If you've enabled Groovy scripting, I believe they should work. Note that this won't work by default in scripted fields in Kibana, since those are currently limited to Lucene query expressions, which apparently don't have a way to query the current date. You have to go out of your way to enable and use Groovy scripts.

---

<div class="post-metadata">

**Author:** ![johan1](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@johan1](https://discuss.elastic.co/u/johan1)\
**Post date:** [April 25, 2016, 5:49am UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/5 "2016-04-25T05:49:53Z")

</div>

Yeah, it seems I'm trying to do something that is not supported yet. Will try to find another way of doing it. Thanks for your help.

---

<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, 1:55pm UTC](https://discuss.elastic.co/t/access-current-time-in-kibana-scripted-field/47897/6 "2017-07-06T13:55:15Z")

</div>


