# Convert Date to String using Painless

**URL:** <https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703>\
**Category:** Kibana\
**Created:** [March 27, 2018, 6:58am UTC](https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703 "2018-03-27T06:58:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahadevgd](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sahadevgd](https://discuss.elastic.co/u/sahadevgd)\
**Post date:** [March 27, 2018, 6:58am UTC](https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703/1 "2018-03-27T06:58:31Z")

</div>

HI Team,

We are on ELK 6.2.3. We have a field "ETime" which is in `UTC` format (`2018-03-27 06:38:08.496Z`).

We would like to convert this time to `HH:mm:ss` and convert that to string using `Painless` language to use it in some other visualization type. I tried with the below code using `SimpleDateFormat` but get an error - Unable to parse.

Code:

```
def mydate = doc['ETime'].value;
def df = new SimpleDateFormat('HH:mm:ss');
String finaldate = df.format(mydate);
return finaldate;

```

[Help - [https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html24](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html24)]

Could you please help on how to convert this field and have it as `Scripted Field`. Correct me if there are any mistakes in the code.

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [March 27, 2018, 8:29pm UTC](https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703/2 "2018-03-27T20:29:46Z")

</div>

The error that you are going to get with this script is:

```auto
"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "Cannot format given Object as a Date"
}

```

When I search in Google to find solutions to that error, I see an article that says the `SimpleDateFormat#format` method expects to have a Date input, but instead a String has been given.

You'll need to first parse the date field (string) as a Date object, then format that date object. It'll be **something** like:

```auto
def dateString = doc['date'].value;

def inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
def myDate = inputFormat.parse(dateString);

def outputFormat = new SimpleDateFormat('HH:mm:ss');

return outputFormat.format(myDate);

```

---

<div class="post-metadata">

**Author:** ![sahadevgd](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sahadevgd](https://discuss.elastic.co/u/sahadevgd)\
**Post date:** [March 28, 2018, 10:11am UTC](https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703/3 "2018-03-28T10:11:59Z")

</div>

Hi @tsullivan, thanks for the response and pointing out the mistake in the code.  
Will try this out.

---

<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:** [April 25, 2018, 10:12am UTC](https://discuss.elastic.co/t/convert-date-to-string-using-painless/125703/4 "2018-04-25T10:12:24Z")

</div>

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