# Extract the year from a date

**URL:** https://discuss.elastic.co/t/extract-the-year-from-a-date/95142
**Category:** Elasticsearch
**Created:** [July 31, 2017, 8:30am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142 "2017-07-31T08:30:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![marwa](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@marwa](https://discuss.elastic.co/u/marwa)
#### Post date: [July 31, 2017, 8:30am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/1 "2017-07-31T08:30:50Z")

</div>

Hello,  
i have data stored in elasticsearch with a field called transaction\_date mapped as a date ,this is its structure in the csv file 2/24/2017;14:15:43 and this how it is viewed in kibana April 18th 2016, 15:15:44.000 .In fact , i want to perform calculations using aggregatiuons with scripted field but i want to extract only data of the year 2016 .how can i extract only the year from the date field ???? any help

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 31, 2017, 8:43am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/2 "2017-07-31T08:43:19Z")

</div>

It's better to solve this problem at index time.

If your field is a date field, you can just generate a date histogram with a year size bucket and that will do what you expect.

---

<div class="post-metadata">

### Author: ![marwa](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@marwa](https://discuss.elastic.co/u/marwa)
#### Post date: [July 31, 2017, 8:50am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/3 "2017-07-31T08:50:06Z")

</div>

Hi , thank you for your reply but i didn't get it are you talking about a bucket using kibana ???  
in fact i need to divide my data into two bucket one for the year 2016 and one for the year 2017 , i know i can make it with aggregations using kibana visualization but i need to extract a metric from the buckets i mentionned then perform an other calculation with both results obtained .

I am trying to use scripted metrics with elasticsearch ? i don't know even if this can lead me to my goal ?? i need your consult !!! even more i am confused between aggregations and using painless to perform this calculation !! any help

---

<div class="post-metadata">

### Author: ![marwa](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@marwa](https://discuss.elastic.co/u/marwa)
#### Post date: [July 31, 2017, 9:12am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/4 "2017-07-31T09:12:43Z")

</div>

Any suggestions please ???

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 31, 2017, 10:25am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/5 "2017-07-31T10:25:33Z")

</div>

Please read be patient section at

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

May be your question is more for Kibana? So may be move it to #kibana instead?  
I believe you can do something similar with time series visual builder.

---

<div class="post-metadata">

### Author: ![marwa](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@marwa](https://discuss.elastic.co/u/marwa)
#### Post date: [July 31, 2017, 10:31am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/6 "2017-07-31T10:31:41Z")

</div>

sorry if i hadn't been patient 😉

---

<div class="post-metadata">

### Author: ![Ravi\_Shanker\_Reddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ravi\_Shanker\_Reddy](https://discuss.elastic.co/u/Ravi_Shanker_Reddy)
#### Post date: [August 2, 2017, 8:09am UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/7 "2017-08-02T08:09:50Z")

</div>

Its better to show some sample data along with the output format you need. No one can help with these type of inputs. Be more specific.

---

<div class="post-metadata">

### Author: ![marwa](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@marwa](https://discuss.elastic.co/u/marwa)
#### Post date: [August 2, 2017, 12:12pm UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/8 "2017-08-02T12:12:18Z")

</div>

hi ,  
thankyou for you reply, maybe i get into details in my question but my main purpose is to perform calculations with elasticsearch then store the results . is there any API that allow me to do this ?

---

<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: [August 30, 2017, 12:14pm UTC](https://discuss.elastic.co/t/extract-the-year-from-a-date/95142/9 "2017-08-30T12:14:56Z")

</div>

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