# How to format a date as a string in Painless

**URL:** <https://discuss.elastic.co/t/how-to-format-a-date-as-a-string-in-painless/138618>\
**Category:** Elasticsearch\
**Created:** [July 5, 2018, 2:45am UTC](https://discuss.elastic.co/t/how-to-format-a-date-as-a-string-in-painless/138618 "2018-07-05T02:45:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)\
**Post date:** [July 5, 2018, 2:45am UTC](https://discuss.elastic.co/t/how-to-format-a-date-as-a-string-in-painless/138618/1 "2018-07-05T02:45:38Z")

</div>

Continuing the discussion from [Elasticsearch 6.2 - how to format date (epoch) field in yyyymm string in painless inline scripting](https://discuss.elastic.co/t/elasticsearch-6-2-how-to-format-date-epoch-field-in-yyyymm-string-in-painless-inline-scripting/133734):

Despite the frequent mentions of switching to `java.time` in the docs, my [cloud.elastic.co](http://cloud.elastic.co) hosted elasticsearch 6.3.0 cluster is still using Joda-Time, as is readily demonstrated by any typo: `"lang": "painless", "caused_by": { "type": "illegal_argument_exception", "reason": "Unable to find dynamic method [to_String] with [1] arguments for class [org.joda.time.DateTime]."`

In order to format an Elasticsearch date as a string in Painless, simply call [toString()](http://www.joda.org/joda-time/userguide.html#Freaky_Formatters). For example `doc['myDate'].value.toString('yyyyMM');`

---

<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 2, 2018, 2:45am UTC](https://discuss.elastic.co/t/how-to-format-a-date-as-a-string-in-painless/138618/2 "2018-08-02T02:45:39Z")

</div>

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