# Inconsistent date\_histogram "key\_as\_string" when "format" is set

**URL:** <https://discuss.elastic.co/t/inconsistent-date-histogram-key-as-string-when-format-is-set/253597>\
**Category:** Elasticsearch\
**Created:** [October 28, 2020, 5:25pm UTC](https://discuss.elastic.co/t/inconsistent-date-histogram-key-as-string-when-format-is-set/253597 "2020-10-28T17:25:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Labrot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_labrot/32/2177_2.png) [@Nicolas\_Labrot](https://discuss.elastic.co/u/Nicolas_Labrot)\
**Post date:** [October 28, 2020, 5:25pm UTC](https://discuss.elastic.co/t/inconsistent-date-histogram-key-as-string-when-format-is-set/253597/1 "2020-10-28T17:25:21Z")

</div>

Hello,

I have an issue with date\_histogram on Elastic 7.9.2. The "key\_as\_string" does not seems to be correct.

I have this index:

```auto
    PUT https://localhost:9200/test -d '
    {
       "mappings":{
          "properties":{
             "date":{
                "type":"date",
                "format":"yyyy-MM-dd"
             }
          }
       }
    }'

```

Those data:

```auto
    POST https://localhost:9200/test/_doc/1 -d '{"date": "2022-02-28"}'
    POST https://localhost:9200/test/_doc/2 -d '{"date": "2022-06-28"}'
    POST https://localhost:9200/test/_doc/3 -d '{"date": "2022-10-28"}'

    POST https://localhost:9200/test/_doc/4 -d '{"date": "2021-02-28"}'
    POST https://localhost:9200/test/_doc/5 -d '{"date": "2021-06-28"}'
    POST https://localhost:9200/test/_doc/6 -d '{"date": "2021-10-28"}'

    POST https://localhost:9200/test/_doc/7 -d '{"date": "2020-02-28"}'
    POST https://localhost:9200/test/_doc/8 -d '{"date": "2020-06-28"}'
    POST https://localhost:9200/test/_doc/9 -d '{"date": "2020-10-28"}'

```

This query:

```auto
    {
      "from": 0,
      "size": 0,
      "query": {
        "match_all": {}
      },
      "aggs": {
        "date": {
          "date_histogram": {
            "field": "date",
            "format": "YYYY-MM-dd",
            "calendar_interval": "1y"
          }
        }
      }
    }

```

Elasticsearch is returning the hereafter result.

```auto
     "aggregations": {
       "date": {
         "buckets": [
           {
             "key_as_string": "2020-01-01",
             "key": 1577836800000,
             "doc_count": 3}
    , {
             "key_as_string": "2020-01-01",
             "key": 1609459200000,
             "doc_count": 3}
    , {
             "key_as_string": "2021-01-01",
             "key": 1640995200000,
             "doc_count": 3}]}}

```

"key\_as\_string" is wrong whereas the key is correct. Without setting the "format" in the aggregation, the "key\_as\_string" is correct.

What could be the issue?

---

<div class="post-metadata">

**Author:** ![Nicolas\_Labrot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_labrot/32/2177_2.png) [@Nicolas\_Labrot](https://discuss.elastic.co/u/Nicolas_Labrot)\
**Post date:** [October 28, 2020, 6:00pm UTC](https://discuss.elastic.co/t/inconsistent-date-histogram-key-as-string-when-format-is-set/253597/2 "2020-10-28T18:00:44Z")

</div>

I do the mistake to use YYYY instead of yyyy 🙂

---

<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 25, 2020, 6:00pm UTC](https://discuss.elastic.co/t/inconsistent-date-histogram-key-as-string-when-format-is-set/253597/3 "2020-11-25T18:00:48Z")

</div>

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