# Script to compare dates

**URL:** <https://discuss.elastic.co/t/script-to-compare-dates/143550>\
**Category:** Elasticsearch\
**Created:** [August 8, 2018, 2:54pm UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550 "2018-08-08T14:54:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [August 8, 2018, 2:54pm UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/1 "2018-08-08T14:54:59Z")

</div>

I would like to compare an elastic search date with a format `YYY-MM-DD HH:MM:SS` as to compare the results of the past 30 days. So if the given date is equal to NOW()-30D it should give me the result wanted. I have tried comparing it this way with a script but it doesn't work :

`getMillis(doc["date"].value) >= (new Date().getTime() - 30 * 24 * 60 * 60 * 1000) ? 1 : 0`

Another test not working :

`doc["date"].value >= (new Date().getTime() - 30) ? 1 : 0`

This should do this : `If doc["date"].value` is higher or equal to the last 30 days it should give me 1, else it would give me 2.

How can I achieve that?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 8, 2018, 3:46pm UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/2 "2018-08-08T15:46:17Z")

</div>

What kind of script are you trying to use?

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [August 9, 2018, 7:02am UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/3 "2018-08-09T07:02:21Z")

</div>

@rjernst I'm using groovy scripting 🙂

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 9, 2018, 7:15pm UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/4 "2018-08-09T19:15:48Z")

</div>

What version of elasticsearch? I recommend switching to elasticsearch 5+, as that has painless, which has the java 8 time api available, and is much easier to work with IMO.

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [August 10, 2018, 7:08am UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/5 "2018-08-10T07:08:16Z")

</div>

@rjernst I'm limited to Elasticsearch 1.9 as the server of my company won't handle a higher version. There isn't a way to do it with groovy? As I suppose comparing an elasticsearch date with the date of the last 30 days should be doable.

---

<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:** [September 7, 2018, 7:08am UTC](https://discuss.elastic.co/t/script-to-compare-dates/143550/6 "2018-09-07T07:08:20Z")

</div>

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