If else return scripted field date field

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.

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

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 }
        """
      }
    }
  }
}

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

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