# Date to string using localtime

**URL:** <https://discuss.elastic.co/t/date-to-string-using-localtime/288360>\
**Category:** Logstash\
**Created:** [November 3, 2021, 7:19pm UTC](https://discuss.elastic.co/t/date-to-string-using-localtime/288360 "2021-11-03T19:19:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticLiver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticliver/32/64869_2.png) [@ElasticLiver](https://discuss.elastic.co/u/ElasticLiver)\
**Post date:** [November 3, 2021, 7:19pm UTC](https://discuss.elastic.co/t/date-to-string-using-localtime/288360/1 "2021-11-03T19:19:52Z")

</div>

Hi, Im trying to convert a date to string with timezone different from UTC

```auto
tz = "-03:00"

t = event.get("lastHeartbeat")
event.set("last_heartbeat_text", Time.at(t.to_f).localtime(tz))

```

and no matter what time is in lastHearbeat allways I get the following

`1970-01-01T00:33:41.000Z`

![image](https://us1.discourse-cdn.com/elastic/original/3X/3/2/32dd0f36973b2e5006ab7dea8d18211698eb46a9.png)

If I just left the variable on event set

`event.set("last_heartbeat_text", t )`

I get the following:

![image](https://us1.discourse-cdn.com/elastic/original/3X/1/0/1097fb7bbab7b2db9ed9289f22937aac6fecd8e1.png)

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [November 3, 2021, 7:53pm UTC](https://discuss.elastic.co/t/date-to-string-using-localtime/288360/2 "2021-11-03T19:53:40Z")

</div>

If I do

```
            t = event.get("@timestamp").to_s
            event.set("last_heartbeat_text", Time.at(t.to_f).localtime(tz))

```

then I get

```
"last_heartbeat_text" => 1970-01-01T00:33:41.000Z,

```

So I think the problem is that [lastHeartbeat] is a string, not a LogStash::Timestamp. Use a date filter to parse and overwrite it.

---

<div class="post-metadata">

**Author:** ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)\
**Post date:** [November 3, 2021, 9:16pm UTC](https://discuss.elastic.co/t/date-to-string-using-localtime/288360/3 "2021-11-03T21:16:46Z")

</div>

Perheps this?

```auto
 date {
   match => ["lastHeartbeat", "yyyy-MM-dd HH:mm:ss.SSS"]
   timezone => "America/Detroit"
}

```

---

<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 1, 2021, 9:17pm UTC](https://discuss.elastic.co/t/date-to-string-using-localtime/288360/4 "2021-12-01T21:17:08Z")

</div>

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