# Scripted Field for Date to Quarter and adding names

**URL:** <https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053>\
**Category:** Kibana\
**Created:** [December 2, 2018, 5:45pm UTC](https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053 "2018-12-02T17:45:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![whyapenny](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whyapenny](https://discuss.elastic.co/u/whyapenny)\
**Post date:** [December 2, 2018, 5:45pm UTC](https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053/1 "2018-12-02T17:45:06Z")

</div>

Im pretty new to scripted fields. I read the elastic blog post on it and checked lucene capabilities. So far I have the simple scripted field to convert the date to month.  
doc['@timestamp'].date.monthOfYear  
which returns 1-12. what I'd like to do 2-fold.

1. Convert the number to the name of the month or at least add the name to it. aka 1 --\> January or to 1-January.

2. I'd like to create another scripted field which actually returns 1-4 for accounting quarterly reports. aka Jan-Mar=1, Apr-Jun=2, Jul-Sep=3 and finally Oct-Dec=4 I know accounting quarters may actually be slightly different, but im sure solution could easily be modified to set the proper values if they are indeed different.

thanks for any help.

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [December 4, 2018, 1:42am UTC](https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053/2 "2018-12-04T01:42:51Z")

</div>

Something like this should work:

```auto
def month = doc['timestamp'].value.monthOfYear;

def months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 

return months[month-1];

```

---

<div class="post-metadata">

**Author:** ![whyapenny](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whyapenny](https://discuss.elastic.co/u/whyapenny)\
**Post date:** [December 4, 2018, 2:55am UTC](https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053/3 "2018-12-04T02:55:11Z")

</div>

cool, so i did the following:  
type=string

scripted field:

```auto
def month = doc['timestamp'].value.monthOfYear;

def months = ['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December']; 

return months[month-1];

```

That way its sortable as well. For the quarterly bit I did a very similar bit except:

```auto
def month = doc['timestamp'].value.monthOfYear;

def quarters = ['01', '01', '01', '02', '02', '02', '03', '03', '03', '04', '04', '04']; 

return quarters[month-1];

```

Thank you for the help here!

---

<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:** [January 1, 2019, 2:55am UTC](https://discuss.elastic.co/t/scripted-field-for-date-to-quarter-and-adding-names/159053/4 "2019-01-01T02:55:20Z")

</div>

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