# If else return scripted field date field

**URL:** <https://discuss.elastic.co/t/if-else-return-scripted-field-date-field/286646>\
**Category:** Kibana\
**Created:** [October 13, 2021, 3:53pm UTC](https://discuss.elastic.co/t/if-else-return-scripted-field-date-field/286646 "2021-10-13T15:53:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya\_Raviraj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surya_raviraj/32/77606_2.png) [@Surya\_Raviraj](https://discuss.elastic.co/u/Surya_Raviraj)\
**Post date:** [October 13, 2021, 3:53pm UTC](https://discuss.elastic.co/t/if-else-return-scripted-field-date-field/286646/1 "2021-10-13T15:53:43Z")

</div>

Is it possible to do an if else return statement with **dates**? For example, if the date in the date column is 1/1/21 I want it to return Week 1. I tried doing that below but I am getting the error shown in the screenshot.

 ![Screen Shot 2021-10-13 at 10.52.18 AM](https://us1.discourse-cdn.com/elastic/original/3X/8/6/86ef1865ac9abe5ff977f044dc2c02b0be289e0f.png)

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [October 14, 2021, 12:29pm UTC](https://discuss.elastic.co/t/if-else-return-scripted-field-date-field/286646/2 "2021-10-14T12:29:36Z")

</div>

You need to format your date into a defined pattern using `DateTimeFormatter`, this is my test using the `kibana_sample_data_flights` dataset:

```auto
GET kibana_sample_data_flights/_search
{
  "size": 5,
  "_source": ["timestamp"],
  "script_fields": {
    "mmddyyy": {
      "script": {
        "source": """
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
dtf.format(doc['timestamp'].value)
        """
      }
    },
    "is08162021": {
      "script": {
        "source": """
String myDate = '08/16/2021';
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
if (dtf.format(doc['timestamp'].value) == myDate){return "yeah, it is " + myDate }
        """
      }
    }
  }
}

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/5/a5ec77fc70c25c7d2003008d7df055895efa2246.png)

More details on how to work with dates on the painless docs:

> **[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:** [November 11, 2021, 12:29pm UTC](https://discuss.elastic.co/t/if-else-return-scripted-field-date-field/286646/3 "2021-11-11T12:29:48Z")

</div>

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