# Compare two date/timestamp in Kibana

**URL:** <https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908>\
**Category:** Kibana\
**Created:** [June 10, 2017, 8:52am UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908 "2017-06-10T08:52:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [June 10, 2017, 8:52am UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/1 "2017-06-10T08:52:42Z")

</div>

Hello,

Is it possible to create a new scripted field by subtracting a date from current time?  
for example:

current timestamp - doc['OpenTime'].value

I tried following code but didn't work.

`doc['@timestamp'].value - doc['OpenTime'].value`

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [June 12, 2017, 4:15pm UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/2 "2017-06-12T16:15:34Z")

</div>

You can get the current timestamp using `new Date().getTime()`. I was able to calculate the difference in millis using the following painless scripted field:

`new Date().getTime() - doc['@timestamp'].value.getMillis()`

 ![](https://us1.discourse-cdn.com/elastic/original/3X/a/5/a5ef322569b403a051d6dd1211bd4e7254d8fdb8.png) ![](https://us1.discourse-cdn.com/elastic/original/3X/a/b/abea8b5cedbd715e6478c6633c806842b38317a7.png)

Using `doc['@timestamp'].value` won't give you the current datetime in real time. In my example, my index has a field called `@timestamp` which is when the docs were indexed.

---

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [June 13, 2017, 6:06am UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/3 "2017-06-13T06:06:44Z")

</div>

Thanks Stacey for your reply.

`new Date().getTime()` works fine for me however when I tried to get time difference, it's not working for me. I used same code `new Date().getTime() - doc['@timestamp'].value.getMillis()`. I have done following, please let me know if I am doing something wrong here.

Language: painless  
Type: Number  
Format: default  
Popularity: 0  
Script: `new Date().getTime() - doc['@timestamp'].value.getMillis()`

![](https://us1.discourse-cdn.com/elastic/original/3X/a/c/ac4bb71dd29ba3bcb35a8011b9359e012e29009c.PNG)

I am using Kibana version 5.3.0

Facing following error.

![](https://us1.discourse-cdn.com/elastic/original/3X/8/d/8d733ba3cc88b4079be19c9a4627199150d58caa.PNG)

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [June 13, 2017, 12:58pm UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/4 "2017-06-13T12:58:35Z")

</div>

I think you want

`new Date().getTime() - doc['OpenTime'].value.getMillis()`

since in your example your time field looks to be called `OpenTime`. I only used `@timestamp` as that's specific to the index I'm testing this with.

If that doesn't work, try

`new Date().getTime() - doc['OpenTime'].value`

I switched to 5.3 and in that version the first variation didn't work as my field appears to be a long type and not a joda MutableDateTime object, so it may have changed in a more recent version.

---

<div class="post-metadata">

**Author:** ![Gurveer\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Gurveer\_Singh](https://discuss.elastic.co/u/Gurveer_Singh)\
**Post date:** [June 13, 2017, 6:41pm UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/5 "2017-06-13T18:41:26Z")

</div>

Yes, you are right I wanted `new Date().getTime() - doc['OpenTime'].value` and this works perfectly.

Adding to this, i wanted to calculate days difference between OpenTime and Current time. So I used following code and works fine for me.

```
def diff = new Date().getTime() - doc['OpenTime'].value;
return diff / 1000 / 60 / 60 / 24

```

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 11, 2017, 6:41pm UTC](https://discuss.elastic.co/t/compare-two-date-timestamp-in-kibana/88908/6 "2017-07-11T18:41:29Z")

</div>

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