# Convert text field to date

**URL:** <https://discuss.elastic.co/t/convert-text-field-to-date/320721>\
**Category:** Logstash\
**Created:** [December 7, 2022, 9:56pm UTC](https://discuss.elastic.co/t/convert-text-field-to-date/320721 "2022-12-07T21:56:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanocog](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@stefanocog](https://discuss.elastic.co/u/stefanocog)\
**Post date:** [December 7, 2022, 9:56pm UTC](https://discuss.elastic.co/t/convert-text-field-to-date/320721/1 "2022-12-07T21:56:14Z")

</div>

Hello,  
I need convert a field in text format into date, i need to have another date field besides `@timestamp`, the `@timestamp` field identifies the arrival time of the log, instead the `dataRisposta` field (in case of response) identifies the time in which the log was generated, I need it to do find the difference between the time of request (other similar field in other log) and response.  
I have this filter

```auto
filter {
  grok {
    match => { "message" => "\"%{TIMESTAMP_ISO8601:timestamp}........"" }
  }
  mutate {
    rename => { "timestamp" => "dataRisposta" }
  }
}

```

And have this result

![2022-12-07_22-38](https://us1.discourse-cdn.com/elastic/original/3X/4/7/47c8b9dc343d886b17d01013e50545a7f7510f58.png)

as you can see the `dataRisposta` field corresponds to the time of the original event, instead the `@timestamp` is the arrival time of the log on elasticsearch.  
Is it possible to convert `dataRisposta` field to date format?

Thanks

---

<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:** [December 7, 2022, 10:20pm UTC](https://discuss.elastic.co/t/convert-text-field-to-date/320721/2 "2022-12-07T22:20:24Z")

</div>

You would use a date filter. To calculate the difference I would use a ruby filter....

```
input { generator { count => 1 lines => ['2022-11-28 09:00:00.123 2022-11-28 09:10:01.000'] } }

output { stdout { codec => rubydebug { metadata => false } } }
filter {
    grok { match => { "message" => "%{TIMESTAMP_ISO8601:t1} %{TIMESTAMP_ISO8601:t2}" } }
    date { match => ["t1", "ISO8601"] target => "t1" }
    date { match => ["t2", "ISO8601"] target => "t2" }
    ruby {
        code => '
            t1 = event.get("t1")
            t2 = event.get("t2")
            if t1 and t2
                event.set("delta", t2.to_f - t1.to_f)
            end
        '
    }

```

which produces

```
     "delta" => 600.8770000934601,

```

---

<div class="post-metadata">

**Author:** ![stefanocog](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@stefanocog](https://discuss.elastic.co/u/stefanocog)\
**Post date:** [December 7, 2022, 10:36pm UTC](https://discuss.elastic.co/t/convert-text-field-to-date/320721/3 "2022-12-07T22:36:21Z")

</div>

I have t1 and t2 in two different logs, because I have request e reponse in two different event

---

<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:** [January 4, 2023, 10:37pm UTC](https://discuss.elastic.co/t/convert-text-field-to-date/320721/4 "2023-01-04T22:37:05Z")

</div>

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