# Bucket keys for date histogram aggregation don't respect timezone

**URL:** <https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437>\
**Category:** Elasticsearch\
**Created:** [January 5, 2016, 7:45pm UTC](https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437 "2016-01-05T19:45:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [January 5, 2016, 7:45pm UTC](https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437/1 "2016-01-05T19:45:28Z")

</div>

I'm trying to do a date histogram using Spring Data Elasticsearch, allowing a customizable time zone. I'm seeing strange behavior. The doc counts in each bucket change with the time zone, which seems to indicate that the time zone is being used in some way. But the bucket key still seems to stay in GMT. I tried doing this query using the search API just to make sure it wasn't a problem with my Java code:

```
{
  "aggs": {
    "by_month": {
      "date_histogram": {
        "field": "dates.created",
        "interval": "month",
        "time_zone": "America/New_York"
      }
    }
  }
}

```

And here is a sample of the results:

```
    {
       "key_as_string": "2015-11-01T00:00:00.000Z",
       "key": 1446336000000,
       "doc_count": 300
    },
    {
       "key_as_string": "2015-12-01T00:00:00.000Z",
       "key": 1448928000000,
       "doc_count": 500
    },

```

Note that these times (both the `key_as_string` and `key`) are at midnight GMT instead of EST. Is there something I'm doing wrong here, or is this expected behavior? My Elasticsearch cluster is running version 1.7.1 if that helps at all.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [January 6, 2016, 10:00am UTC](https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437/2 "2016-01-06T10:00:21Z")

</div>

I would advise against using the time\_zone parameter in 1.7 since it is quite buggy. However it got fixed in Elasticsearch 2.0. What this parameter does is that it computes buckets based on the given time zone, eg a daily aggregation will project all date values to midnight in the specified timezone instead of UTC. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking\_20\_aggregation\_changes.html#\_time\_zones\_and\_offsets](https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_20_aggregation_changes.html#_time_zones_and_offsets) and [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#\_time\_zone](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#_time_zone) for more information.

---

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [January 7, 2016, 3:57pm UTC](https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437/3 "2016-01-07T15:57:44Z")

</div>

Thanks for your reply. Is there any 1.x version where it works properly? Or is there some equivalent in 1.x versions (based on my digging around, perhaps `pre_zone` and `post_zone`)?

---

<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 5, 2017, 11:26pm UTC](https://discuss.elastic.co/t/bucket-keys-for-date-histogram-aggregation-dont-respect-timezone/38437/4 "2017-07-05T23:26:21Z")

</div>


