# How to get MonthYear from DateTime using Painless

**URL:** <https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106>\
**Category:** Kibana\
**Created:** [February 12, 2021, 10:41am UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106 "2021-02-12T10:41:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Rayabarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakesh_rayabarapu/32/79132_2.png) [@Rakesh\_Rayabarapu](https://discuss.elastic.co/u/Rakesh_Rayabarapu)\
**Post date:** [February 12, 2021, 10:41am UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106/1 "2021-02-12T10:41:27Z")

</div>

Hello team,

I wrote a script to get month (string) = doc['@timstamp'].value.getMonth().  
Year=doc['@timstamp'].value.getYear().

Used concatenation montheYear=doc['@timstamp'].value.getMonth()+doc['@timstamp'].value.getYear().

Error: String + integer is not possible .

please help me to get year in string or else any other way we have to get monthyear (eg;Jan2021).

Thanks in advance

Regards  
Rakesh R

---

<div class="post-metadata">

**Author:** ![Patrick\_Mueller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/patrick_mueller/32/109425_2.png) [@Patrick\_Mueller](https://discuss.elastic.co/u/Patrick_Mueller)\
**Post date:** [February 22, 2021, 5:03pm UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106/2 "2021-02-22T17:03:10Z")

</div>

Have you tried casting the year, or using String.format() or casting? Eg

```auto
montheYear = doc['@timstamp'].value.getMonth() + String.format("%d", doc['@timstamp'].value.getYear())

```

---

<div class="post-metadata">

**Author:** ![Rakesh\_Rayabarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakesh_rayabarapu/32/79132_2.png) [@Rakesh\_Rayabarapu](https://discuss.elastic.co/u/Rakesh_Rayabarapu)\
**Post date:** [March 2, 2021, 9:00am UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106/3 "2021-03-02T09:00:33Z")

</div>

Thank you Patrick for responding..  
Could you please give one Eg for casting?  
Here is the error with String . format

{  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"doc['TimeStamp'].value.getMonth() + String.format("%d", doc['TimeStamp'].value.getYear())",  
" ^---- HERE"  
],  
"script": "doc['TimeStamp'].value.getMonth() + String.format("%d", doc['TimeStamp'].value.getYear())",  
"lang": "painless",  
"position": {  
"offset": 78,  
"start": 0,  
"end": 89  
}  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "iq",  
"node": "uJDR1PCXTdSJpz--zeIPBg",  
"reason": {  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"doc['TimeStamp'].value.getMonth() + String.format("%d", doc['TimeStamp'].value.getYear())",  
" ^---- HERE"  
],  
"script": "doc['TimeStamp'].value.getMonth() + String.format("%d", doc['TimeStamp'].value.getYear())",  
"lang": "painless",  
"position": {  
"offset": 78,  
"start": 0,  
"end": 89  
},  
"caused\_by": {  
"type": "wrong\_method\_type\_exception",  
"reason": "cannot convert MethodHandle(JodaCompatibleZonedDateTime)int to (Object)Object"  
}  
}  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Patrick\_Mueller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/patrick_mueller/32/109425_2.png) [@Patrick\_Mueller](https://discuss.elastic.co/u/Patrick_Mueller)\
**Post date:** [March 2, 2021, 2:23pm UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106/4 "2021-03-02T14:23:34Z")

</div>

I mentioned casting in case you were familiar with it, but you should use the `String.format()` method if you're not familiar with casting.

The problem appears to be with the `doc['TimeStamp'].value.getMonth()` call. Here's some doc on working with date fields in Painless: [Using Datetime in Painless | Painless Scripting Language [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-datetime.html)

---

<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:** [March 30, 2021, 2:23pm UTC](https://discuss.elastic.co/t/how-to-get-monthyear-from-datetime-using-painless/264106/5 "2021-03-30T14:23:43Z")

</div>

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