# Date\_time\_exception: Invalid value for MonthOfYear (valid values 1 - 12): 28

**URL:** <https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208>\
**Category:** Elasticsearch\
**Created:** [August 30, 2022, 4:39am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208 "2022-08-30T04:39:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gex1](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gex1](https://discuss.elastic.co/u/Gex1)\
**Post date:** [August 30, 2022, 4:39am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/1 "2022-08-30T04:39:15Z")

</div>

I am using ES 7.10.0  
In my case I expect date values like:

Case1: 28/09/2022:02:00:00  
Case2: 09/28/2022:02:00:00  
Case3: 2022-09-28T02:00:00

Below is the template, which I am using:

```auto
PUT _template/template_1
{
  "index_patterns": ["test*"],
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "created_at": {
        "type": "date",
        "format": "dd/MM/yyyy:HH:mm:ss||MM/dd/yyyy:HH:mm:ss||strict_date_optional_time"
      }
    }
  }
}

```

The above mapping works for Case1 and Case3 but it throws an exception for Case2 due to which the multi-format date thing does not gets supported.

Here is my Input

```auto
PUT test1/_doc/1
{
  "created_at": "28/09/2022:02:00:00"
}

PUT test1/_doc/2
{
  "created_at": "09/28/2022:02:00:00"
}

PUT test1/_doc/3
{
  "created_at": "2022-09-28T02:00:00"
}

```

The Error Message:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "date_time_exception",
        "reason": "date_time_exception: Invalid value for MonthOfYear (valid values 1 - 12): 28"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [created_at] of type [date] in document with id '3'. Preview of field's value: '09/28/2022:02:00:00'",
    "caused_by": {
      "type": "date_time_exception",
      "reason": "date_time_exception: Invalid value for MonthOfYear (valid values 1 - 12): 28"
    }
  },
  "status": 400
}

```

So for whichever date format (either Case1 or Case2) is given first in the mappings, ES supports it and for the other one it creates an exception resulting in multi-format feature not to work

Is there any workaround for this from the mapping perspective?  
Kindly help with the same, Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 30, 2022, 5:14am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/2 "2022-08-30T05:14:19Z")

</div>

It looks like a bug to me. Could you please open an issue and provide the same great description you did here?

Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 30, 2022, 5:20am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/3 "2022-08-30T05:20:23Z")

</div>

> [@Gex1](#):
>
> Case1: 28/09/2022:02:00:00  
> Case2: 09/28/2022:02:00:00

If you expect to handle date values like that, what do you expect the date value to be for the following?

Case 4: 08/09/2022:02:00:00

Should it be parsed the same as Case 1 or Case 2?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 30, 2022, 5:45am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/4 "2022-08-30T05:45:37Z")

</div>

Very true. I'd call it a random date. Let's hope aggregations or searches are only on years 😉

---

<div class="post-metadata">

**Author:** ![Gex1](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gex1](https://discuss.elastic.co/u/Gex1)\
**Post date:** [August 30, 2022, 6:02am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/5 "2022-08-30T06:02:19Z")

</div>

True enough.

Do you all think there could be any solution for this?

One more query:  
Let's say these formats are decided by some other key value condition in the record, so based on that can we apply the different date formats in the mappings ?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 30, 2022, 6:03am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/6 "2022-08-30T06:03:55Z")

</div>

If you can tell based on other fields I would recommend transforming the date to a common format before indexing it. You can not use other fields in mappings.

---

<div class="post-metadata">

**Author:** ![Gex1](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gex1](https://discuss.elastic.co/u/Gex1)\
**Post date:** [August 30, 2022, 7:00am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/7 "2022-08-30T07:00:17Z")

</div>

Thanks a lot @Christian_Dahlqvist, @dadoonet for all the help

Btw, what do you guys feel about this, are we expecting any new feature upcoming with regards Template/Mapping which gives possibility like changing field\_types or formats (date formats etc). based on some (Key value) conditions at the template/mapping level (may be by using painless script). So that it gives complete control from template end as well?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 30, 2022, 7:03am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/8 "2022-08-30T07:03:49Z")

</div>

Given that analysis is performed at indexing as well as query time I think any fundamental change like that is quite unlikely. The correct way is to clean up your data before indexing it.

---

<div class="post-metadata">

**Author:** ![Gex1](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gex1](https://discuss.elastic.co/u/Gex1)\
**Post date:** [August 30, 2022, 7:12am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/9 "2022-08-30T07:12:28Z")

</div>

Thanks again @Christian_Dahlqvist . And in the case if we don't have an other differentiating (field value pair ) for the different date formats, then we won't be able to reach to a proper solution, right?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 30, 2022, 7:40am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/10 "2022-08-30T07:40:27Z")

</div>

If you can not tell how the timestamp is to be interpreted, how would Elasticsearch be able to handle it? If you do not have anything in the data itself you may need to reformat/interpret based on the source of the data and your knowledge of which format it uses.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 30, 2022, 7:45am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/11 "2022-08-30T07:45:43Z")

</div>

I think it might be possible with runtime fields...

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 30, 2022, 7:50am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/12 "2022-08-30T07:50:14Z")

</div>

Good point. That may work as long as there is another field to trigger on or the format is unambigous. That would require upgrading to a newer version though, wouldn't it?

---

<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:** [September 27, 2022, 7:50am UTC](https://discuss.elastic.co/t/date-time-exception-invalid-value-for-monthofyear-valid-values-1-12-28/313208/13 "2022-09-27T07:50:43Z")

</div>

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