# How to retain the time zone and nanoseconds in timestamp when migrating es through logstash

**URL:** <https://discuss.elastic.co/t/how-to-retain-the-time-zone-and-nanoseconds-in-timestamp-when-migrating-es-through-logstash/368002>\
**Category:** Logstash\
**Created:** [September 30, 2024, 9:27am UTC](https://discuss.elastic.co/t/how-to-retain-the-time-zone-and-nanoseconds-in-timestamp-when-migrating-es-through-logstash/368002 "2024-09-30T09:27:51Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [September 30, 2024, 10:33am UTC](https://discuss.elastic.co/t/how-to-retain-the-time-zone-and-nanoseconds-in-timestamp-when-migrating-es-through-logstash/368002/2 "2024-09-30T10:33:32Z")

</div>

This was explained [here](https://discuss.elastic.co/t/logstash-failed-to-parse-date-6-milliseconds/299271).

Credits to Badger

Here is a sample:

```auto
input {
 generator { 
        # message => ["2024-08-12T09:40:31.098Z"]
        message => ["2024-08-12T17:40:31.098422413+08:00"]
        count => 1
 }
}
output {
 stdout {codec => rubydebug}
}
filter {

date {
    match => ["message", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSZ"] # "ISO8601"
    timezone => "Asia/Shanghai"
    target => "@timestamp"
  }
  ruby {
    code => "event.set('[longformat]', LogStash::Timestamp.new(event.get('[message]')) )"
  }
     mutate { remove_field => ["@version", "event", "host"] }
}

```

Output:

```auto
{
       "message" => "2024-08-12T17:40:31.098422413+08:00",
    "@timestamp" => 2024-08-12T09:40:31.098Z,
    "longformat" => 2024-08-12T09:40:31.098422413Z
}

```

---

_[View the full topic](https://discuss.elastic.co/t/how-to-retain-the-time-zone-and-nanoseconds-in-timestamp-when-migrating-es-through-logstash/368002)._
