# Watcher, Transform UTC timestamp on the payload to local time

**URL:** <https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [September 24, 2020, 1:35am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724 "2020-09-24T01:35:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Incauto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/incauto/32/60149_2.png) [@Incauto](https://discuss.elastic.co/u/Incauto)\
**Post date:** [September 24, 2020, 1:35am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724/1 "2020-09-24T01:35:22Z")

</div>

In the payload of a watcher I have the timestamp field

```auto
"@timestamp" : "2020-09-23T23:11:24.359Z",

```

But this field is in UTC, and I want to change it to local time, I have tried the folowing

```auto
  "actions": {
    "send_email": {
      "transform": {
        "script": """
return ['date': Instant.ofEpochMilli(ctx.payload.hits.hits.0._source.@timestamp).atZone(ZoneId.of("America/Lima"))]"""
      },

```

but it trows me an error

```auto
    "reason" : "compile error",
    "script_stack" : [
      "... load.hits.hits.0._source.@timestamp).atZone(ZoneId ...",
      " ^---- HERE"
    ],

```

What should I do to make it work?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 24, 2020, 1:39am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724/2 "2020-09-24T01:39:31Z")

</div>

I think you may need to use `ctx.payload.hits.hits.0._source['@timestamp']` in the script

---

<div class="post-metadata">

**Author:** ![Incauto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/incauto/32/60149_2.png) [@Incauto](https://discuss.elastic.co/u/Incauto)\
**Post date:** [September 24, 2020, 1:48am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724/3 "2020-09-24T01:48:22Z")

</div>

Thanks Russ, that worked.....but now It gives me another error

```auto
"caused_by" : {
                "type" : "class_cast_exception",
                "reason" : "class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')"
              }

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 24, 2020, 6:08am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724/4 "2020-09-24T06:08:08Z")

</div>

The `@timestamp` is in ISO8601 string format, so in order to parse this into a `ZonedDateTime`, you'd need to use something like

```auto
ZonedDateTime zdt = ZonedDateTime.parse(ctx.payload.hits.hits.0._source['@timestamp']);

```

Take a look at the [Datetime parsing and formatting](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html#_datetime_parsing_and_formatting) Painless reference.

---

<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:** [October 22, 2020, 6:08am UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/249724/5 "2020-10-22T06:08:13Z")

</div>

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