# Date not converting to different timezone in painless

**URL:** <https://discuss.elastic.co/t/date-not-converting-to-different-timezone-in-painless/304535>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [May 12, 2022, 7:59am UTC](https://discuss.elastic.co/t/date-not-converting-to-different-timezone-in-painless/304535 "2022-05-12T07:59:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shivendra95](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@shivendra95](https://discuss.elastic.co/u/shivendra95)\
**Post date:** [May 12, 2022, 7:59am UTC](https://discuss.elastic.co/t/date-not-converting-to-different-timezone-in-painless/304535/1 "2022-05-12T07:59:14Z")

</div>

Hi there,  
I have date field in UTC and want to convert that date in to a different timezone and display it.  
I created a new runtime field and made the type as date, but when I convert the date and emit it, the displayed date is the original one and not according to timezone. Below is the script

```auto
String date = '2022-04-27T01:39:29.030+00:00';
ZonedDateTime time1 = ZonedDateTime.parse(date);
Instant instant = time1.toInstant();
ZonedDateTime time2 = instant.atZone(ZoneId.of('America/Chicago')) ;
long a = time2.toInstant().toEpochMilli();
emit(a);

```

The output I'm getting is the same date i.e **2022-04-27T01:39:29.030+00:00** but it should be something like this **2022-04-26T20:39:29.030-05:00[America/Chicago]** according to timezone.

I'm not able to figure out what I'm doing wrong here.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 12, 2022, 9:52pm UTC](https://discuss.elastic.co/t/date-not-converting-to-different-timezone-in-painless/304535/2 "2022-05-12T21:52:35Z")

</div>

Hi @shivendra95

I did a quick test with your script. I had successful.  
Maybe the problem is something else.

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "description": "",
          "lang": "painless",
          "source": """
              String date = '2022-04-27T01:39:29.030+00:00';
              ZonedDateTime time1 = ZonedDateTime.parse(date);
              Instant instant = time1.toInstant();
              ZonedDateTime time2 = instant.atZone(ZoneId.of('America/Chicago')) ;
              ctx.origin_date = time1;
              ctx.new_date = time2;
          """,
          "params": {
            "delimiter": "-",
            "position": 1
          }
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "new_date":""
      }
    }
  ]
}

```

Response:

```auto
        "_source" : {
          "origin_date" : "2022-04-27T01:39:29.030Z",
          "new_date" : "2022-04-26T20:39:29.030-05:00"
        },

```

---

<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:** [June 9, 2022, 9:52pm UTC](https://discuss.elastic.co/t/date-not-converting-to-different-timezone-in-painless/304535/3 "2022-06-09T21:52:54Z")

</div>

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