# Date in script field

**URL:** <https://discuss.elastic.co/t/date-in-script-field/101951>\
**Category:** Elasticsearch\
**Created:** [September 27, 2017, 8:44am UTC](https://discuss.elastic.co/t/date-in-script-field/101951 "2017-09-27T08:44:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirtash](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@Kirtash](https://discuss.elastic.co/u/Kirtash)\
**Post date:** [September 27, 2017, 8:44am UTC](https://discuss.elastic.co/t/date-in-script-field/101951/1 "2017-09-27T08:44:34Z")

</div>

Good morning,

I'm doing examples of script fields and I have read the documentation but I don't understand how I can use date fields. My idea is get the age using scripts:

GET /prova/\_search  
{  
"\_source": {},  
"script\_fields": {  
"a": {  
"script": {  
"lang": "painless",  
"inline": "def a = '2017-09-27T00:00:00.000Z'; return a.date.year-doc['dt'].date.year"  
}  
}  
}  
}

Exist the type Date? I have read that there is: String, list, map etc. but not date, and now when I declare this variable  
is a String, and thisis the cause that I can't do ".date.year

Can you help me please?

Thanks!!

---

<div class="post-metadata">

**Author:** ![Kirtash](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@Kirtash](https://discuss.elastic.co/u/Kirtash)\
**Post date:** [September 28, 2017, 7:06am UTC](https://discuss.elastic.co/t/date-in-script-field/101951/2 "2017-09-28T07:06:49Z")

</div>

Hi!

I have read that the painless use the primitive types of Java and the Date is not one of them, is imposible declare a date.now?

Thanks!

---

<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:** [September 28, 2017, 8:05pm UTC](https://discuss.elastic.co/t/date-in-script-field/101951/3 "2017-09-28T20:05:37Z")

</div>

You need to look at the [Java8 time apis](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). You will need to parse your string date into probably a DateTime, then convert the ReadableDateTime (a joda class) to java 8's DateTime as well in order to compare.

---

<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:** [October 26, 2017, 8:05pm UTC](https://discuss.elastic.co/t/date-in-script-field/101951/4 "2017-10-26T20:05:42Z")

</div>

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