# DateHistogram not working as expected

**URL:** https://discuss.elastic.co/t/datehistogram-not-working-as-expected/185602
**Category:** Elasticsearch
**Created:** [June 13, 2019, 10:02am UTC](https://discuss.elastic.co/t/datehistogram-not-working-as-expected/185602 "2019-06-13T10:02:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![umeshmishra099](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umeshmishra099/32/47939_2.png) [@umeshmishra099](https://discuss.elastic.co/u/umeshmishra099)
#### Post date: [June 13, 2019, 10:02am UTC](https://discuss.elastic.co/t/datehistogram-not-working-as-expected/185602/1 "2019-06-13T10:02:11Z")

</div>

Mapping:

```auto
        {
          "test_2e311eff-97c0-4270-b254-0c43cd5f2acc": {
            "mappings": {
              "test_2e311eff-97c0-4270-b254-0c43cd5f2acc_type": {
                "properties": {
                  "2e311eff-97c0-4270-b254-0c43cd5f2accTableLevelStatsFieldInternalRule_Completeness": {
                    "type": "integer"
                  },
                  "_testile_row_id": {
                    "type": "keyword"
                  },
                  "date22": {
                    "type": "date",
                    "format": "yyyy-MM-dd||yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS'Z'||HH:mm:ss||HH:mm:ss.SSS||date_optional_time||epoch_millis"
                  },
                  "datetime22": {
                    "type": "date",
                    "format": "yyyy-MM-dd||yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS'Z'||HH:mm:ss||HH:mm:ss.SSS||date_optional_time||epoch_millis"
                  },
                  "time22": {
                    "type": "date",
                    "format": "yyyy-MM-dd||yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS'Z'||HH:mm:ss||HH:mm:ss.SSS||date_optional_time||epoch_millis"
                  }
                }
              }
            }
          }
        }

```

Data in index

```auto
{
              "_testile_row_id": "1",
              "time22": "10:00:00",
              "datetime22": "2017-09-01T10:00:00"
            }
          },
          {
            "_index": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc",
            "_type": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc_type",
            "_id": "9ZoLUGsBn7EFIldnql5B",
            "_score": 1,
            "_source": {
              "_testile_row_id": "2",
              "time22": "11:09:00",
              "datetime22": "2017-09-02T22:00:00",
              "date22": null
            }
          },
          {
            "_index": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc",
            "_type": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc_type",
            "_id": "9poLUGsBn7EFIldnql5B",
            "_score": 1,
            "_source": {
              "_testile_row_id": "3",
              "time22": "03:09:00",
              "datetime22": "2017-09-02T10:00:00",
              "date22": "2016-09-01"
            }
          },
          {
            "_index": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc",
            "_type": "test_2e311eff-97c0-4270-b254-0c43cd5f2acc_type",
            "_id": "95oLUGsBn7EFIldnql5B",
            "_score": 1,
            "_source": {
              "_testile_row_id": "4",
              "time22": "15:09:00",
              "datetime22": null,
              "date22": "2016-09-01"
            }
          }
        ]
      }
    }

```

Date histogram aggregation query:

```auto
GET test_2e311eff-97c0-4270-b254-0c43cd5f2acc/_search
    {
      "aggs": {
        "Histogram": {
          "date_histogram": {
            "field": "date22",
            "interval": 677376000,
            "offset": 0,
            "order": {
              "_key": "asc"
            },
            "keyed": false,
            "min_doc_count": 0
          }
        }
      }
    }

```

Response:

```auto
{
  "aggregations": {
    "Histogram": {
      "buckets": [
        {
          "key_as_string": "2016-08-31",
          "key": 1472615424000,
          "doc_count": 2
        },
        {
          "key_as_string": "2016-09-08",
          "key": 1473292800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-09-15",
          "key": 1473970176000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-09-23",
          "key": 1474647552000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-10-01",
          "key": 1475324928000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-10-09",
          "key": 1476002304000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-10-17",
          "key": 1476679680000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-10-25",
          "key": 1477357056000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-11-01",
          "key": 1478034432000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-11-09",
          "key": 1478711808000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-11-17",
          "key": 1479389184000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-11-25",
          "key": 1480066560000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-12-03",
          "key": 1480743936000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-12-11",
          "key": 1481421312000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-12-18",
          "key": 1482098688000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-12-26",
          "key": 1482776064000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-01-03",
          "key": 1483453440000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-01-11",
          "key": 1484130816000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-01-19",
          "key": 1484808192000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-01-27",
          "key": 1485485568000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-02-03",
          "key": 1486162944000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-02-11",
          "key": 1486840320000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-02-19",
          "key": 1487517696000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-02-27",
          "key": 1488195072000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-03-07",
          "key": 1488872448000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-03-15",
          "key": 1489549824000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-03-23",
          "key": 1490227200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-03-30",
          "key": 1490904576000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-04-07",
          "key": 1491581952000,
          "doc_count": 0
        }
      ]
    }
  }
}

```

First bucket should be 2016-09-01

---

<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: [June 13, 2019, 10:16am UTC](https://discuss.elastic.co/t/datehistogram-not-working-as-expected/185602/2 "2019-06-13T10:16:02Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.  
Please update your post.

---

<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: [July 11, 2019, 10:16am UTC](https://discuss.elastic.co/t/datehistogram-not-working-as-expected/185602/3 "2019-07-11T10:16:10Z")

</div>

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