# Format a Scripted Date Field

**URL:** <https://discuss.elastic.co/t/format-a-scripted-date-field/327309>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [March 8, 2023, 9:31pm UTC](https://discuss.elastic.co/t/format-a-scripted-date-field/327309 "2023-03-08T21:31:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashryver1995](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashryver1995/32/82060_2.png) [@ashryver1995](https://discuss.elastic.co/u/ashryver1995)\
**Post date:** [March 8, 2023, 9:31pm UTC](https://discuss.elastic.co/t/format-a-scripted-date-field/327309/1 "2023-03-08T21:31:53Z")

</div>

Hi,

I have a scripted field using painless that is referencing from an existing field in my index. Below is the code snippet:

```auto
if(doc['market'].value == 'SAMPLE'){
    if (doc['date field].size()==0){
        return ''
    } else {
        return doc['last ship date'].value + ("45d")
    }
}

```

In the scripted field setting itself, I set the type as a Date and put in a Custom Format DD-MMM-YY. However when I view it on my search it still has a timestamp format and not the one I defined.

Anyone have any idea on how to resolve this?

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [March 9, 2023, 2:55pm UTC](https://discuss.elastic.co/t/format-a-scripted-date-field/327309/2 "2023-03-09T14:55:54Z")

</div>

Hi Ash. I suggest formatting the date in the Painless script like this.

```auto
if (doc['@timestamp'].size()==0){
    return ''
} else {
    ZonedDateTime input = doc['@timestamp'].value.plusDays(45);
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
    "'date:' yyyy/MM/dd 'time:' HH:mm:ss");
    String output = input.format(dtf);
    return output;
}

```

There's more info in the [Painless Datetime API documentation](https://www.elastic.co/guide/en/elasticsearch/painless/8.6/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:** [April 6, 2023, 2:56pm UTC](https://discuss.elastic.co/t/format-a-scripted-date-field/327309/3 "2023-04-06T14:56:25Z")

</div>

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