# Extract 'Year' from 'new Date()' function in Kibana scripted field

**URL:** <https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463>\
**Category:** Kibana\
**Created:** [August 10, 2020, 8:44pm UTC](https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463 "2020-08-10T20:44:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Smith1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_smith1/32/45072_2.png) [@Mike\_Smith1](https://discuss.elastic.co/u/Mike_Smith1)\
**Post date:** [August 10, 2020, 8:44pm UTC](https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463/1 "2020-08-10T20:44:45Z")

</div>

How can I extract only year (2020) from new Date() function in Kibana scripted field?  
Following is scripted field entry  
new Date()  
Preview of above function displays  
{  
"\_id": "a38be692-7b1c-4d9b-a8eb-cfedb15e6937",  
"Date test": [  
"2020-08-10T18:18:40.822Z"  
]  
},  
Appreciate your help.

---

<div class="post-metadata">

**Author:** ![Aaron\_Caldwell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron_caldwell/32/45755_2.png) [@Aaron\_Caldwell](https://discuss.elastic.co/u/Aaron_Caldwell)\
**Post date:** [August 11, 2020, 12:49pm UTC](https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463/2 "2020-08-11T12:49:54Z")

</div>

Hello Mike,

You should be able to use `new SimpleDateFormat('yyyy')` to grab the year. Reference: [Scripted fields date parse](https://discuss.elastic.co/t/scripted-fields-date-parse/167241).

Regards,  
Aaron

---

<div class="post-metadata">

**Author:** ![Mike\_Smith1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_smith1/32/45072_2.png) [@Mike\_Smith1](https://discuss.elastic.co/u/Mike_Smith1)\
**Post date:** [August 11, 2020, 10:04pm UTC](https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463/3 "2020-08-11T22:04:54Z")

</div>

Thanks Aaron. I applied following in scripted field and got current year.  
scripted field...  
def date = new Date();  
def sdf = new SimpleDateFormat('yyyy');  
return sdf.format(date);

output ....  
[{ "\_id": "a38be692-7b1c-4d9b-a8eb-cfedb15e6937", "Date test": [ "2020"] },

---

<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 8, 2020, 10:05pm UTC](https://discuss.elastic.co/t/extract-year-from-new-date-function-in-kibana-scripted-field/244463/4 "2020-09-08T22:05:06Z")

</div>

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