# Is it possible to apply a timezone to a date in a scripted field?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-apply-a-timezone-to-a-date-in-a-scripted-field/65372>\
**Category:** Elasticsearch\
**Created:** [November 8, 2016, 3:28pm UTC](https://discuss.elastic.co/t/is-it-possible-to-apply-a-timezone-to-a-date-in-a-scripted-field/65372 "2016-11-08T15:28:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ols](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@ols](https://discuss.elastic.co/u/ols)\
**Post date:** [November 8, 2016, 3:28pm UTC](https://discuss.elastic.co/t/is-it-possible-to-apply-a-timezone-to-a-date-in-a-scripted-field/65372/1 "2016-11-08T15:28:57Z")

</div>

In the aggregation below I need the bucketing to take account of a passed in timezone. How do I do this? Painless is java based, no? So is it possible to import some sort of timezone module and use that?

```
  aggs: {
    hourOfDay: {
      terms: {
        script: {
          inline: "doc['created'].date.hourOfDay",
          lang: 'painless',
        },
      },
    },
  },
```

---

<div class="post-metadata">

**Author:** ![ols](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@ols](https://discuss.elastic.co/u/ols)\
**Post date:** [November 24, 2016, 10:48am UTC](https://discuss.elastic.co/t/is-it-possible-to-apply-a-timezone-to-a-date-in-a-scripted-field/65372/2 "2016-11-24T10:48:47Z")

</div>

In the end this was solved using the aggregation below but at the cost of using Groovy in place of Painless and it also means the security risk of having `script.engine.groovy.inline.aggs` enabled. Can anyone suggest a method that is more performant and secure?

Is better support for timezones on the roadmap?

```auto
{
  "size": 0,
  "aggregations": {
    "dayOfWeek": {
      "terms": {
        "script": {
          "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.dayOfWeek",
          "lang": "groovy",
          "params": {
            "tz": "Europe/London"
          }
        }
      },
      "aggs": {
        "hourOfDay": {
          "terms": {
            "script": {
              "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.hourOfDay",
              "lang": "groovy",
              "params": {
                "tz": "Europe/London"
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [December 22, 2016, 10:49am UTC](https://discuss.elastic.co/t/is-it-possible-to-apply-a-timezone-to-a-date-in-a-scripted-field/65372/3 "2016-12-22T10:49:03Z")

</div>

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