# Get current year from painless

**URL:** <https://discuss.elastic.co/t/get-current-year-from-painless/288738>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [November 9, 2021, 9:49am UTC](https://discuss.elastic.co/t/get-current-year-from-painless/288738 "2021-11-09T09:49:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Bozzano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_bozzano/32/73731_2.png) [@Andrea\_Bozzano](https://discuss.elastic.co/u/Andrea_Bozzano)\
**Post date:** [November 9, 2021, 9:49am UTC](https://discuss.elastic.co/t/get-current-year-from-painless/288738/1 "2021-11-09T09:49:23Z")

</div>

Hi everyone!

In my application, I'm trying to retrieve the current year in a scripted field.  
The idea was (in pseudocode):

```auto
calculated = currentYear - doc['field'].value

return calculated;

```

I tried with:

```auto
Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());

```

And actually retrieves the current timestamp, but i can't access the Year value.

Is it possible, and if yes how, to get the current year value in Painless as an integer number?

Thank you for your time and patience!

---

<div class="post-metadata">

**Author:** ![majagrubic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/majagrubic/32/74459_2.png) [@majagrubic](https://discuss.elastic.co/u/majagrubic)\
**Post date:** [November 9, 2021, 10:58am UTC](https://discuss.elastic.co/t/get-current-year-from-painless/288738/2 "2021-11-09T10:58:05Z")

</div>

Assuming your date field is called `field` and is mapped as `date`, this should do the trick:

> String datetime = doc['field'].value.toString();  
> ZonedDateTime zdt = ZonedDateTime.parse(datetime);  
> emit(zdt.getYear());

---

<div class="post-metadata">

**Author:** ![Andrea\_Bozzano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_bozzano/32/73731_2.png) [@Andrea\_Bozzano](https://discuss.elastic.co/u/Andrea_Bozzano)\
**Post date:** [November 9, 2021, 11:11am UTC](https://discuss.elastic.co/t/get-current-year-from-painless/288738/3 "2021-11-09T11:11:17Z")

</div>

Thank you 🙂 This actually worked 🙂

---

<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:** [December 7, 2021, 11:11am UTC](https://discuss.elastic.co/t/get-current-year-from-painless/288738/4 "2021-12-07T11:11:41Z")

</div>

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