# Logstash convert String into milliseconds

**URL:** <https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398>\
**Category:** Logstash\
**Created:** [December 31, 2019, 12:28am UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398 "2019-12-31T00:28:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vijayakrishna.rg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijayakrishna.rg/32/36798_2.png) [@vijayakrishna.rg](https://discuss.elastic.co/u/vijayakrishna.rg)\
**Post date:** [December 31, 2019, 12:28am UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/1 "2019-12-31T00:28:16Z")

</div>

Hi,

I am using logstash to convert below two strings into integer and then converting integer to milliseconds.

i have nginx logs with below values

```
request_time="0.051" 
response_time="0.027"

```

first i am converting them into float

```
  mutate {
    convert => ["request_time", "float"]
    convert => ["response_time", "float"]
  }

```

and them am trying to convert them into milliseconds with below method, but am getting zero printing in the kibana

```
ruby {
  code => "event.set('request_time', event.get('request_time').to_i * 1000)"
}

```

![Screen Shot 2019-12-30 at 4.27.39 PM](https://us1.discourse-cdn.com/elastic/original/3X/8/8/888b040e0e7bef72f18077d65b65def031ee0ade.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:** [December 31, 2019, 3:14pm UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/2 "2019-12-31T15:14:29Z")

</div>

The .to\_i will round them down to zero, I think. Does it work if you use this?

```
(event.get('request_time') * 1000.0).to_i
```

---

<div class="post-metadata">

**Author:** ![vijayakrishna.rg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijayakrishna.rg/32/36798_2.png) [@vijayakrishna.rg](https://discuss.elastic.co/u/vijayakrishna.rg)\
**Post date:** [December 31, 2019, 5:44pm UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/3 "2019-12-31T17:44:32Z")

</div>

Thats works, thanks for suggestion.

i have different issue while adding 2 strings into condition like below.

```
ruby {
code => "event.set('request_time', event.get('request_time') * 1000.0).to_i"
code => "event.set('response_time', event.get('response_time') * 1000.0).to_i"
}

```

is this correct format ?

---

<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 31, 2019, 6:54pm UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/4 "2019-12-31T18:54:15Z")

</div>

Not sure if that works or if one code option overwrites the other. I would use

```
ruby {
    code => "
        event.set('request_time', event.get('request_time') * 1000.0).to_i
        event.set('response_time', event.get('response_time') * 1000.0).to_i
    "
}
```

---

<div class="post-metadata">

**Author:** ![vijayakrishna.rg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijayakrishna.rg/32/36798_2.png) [@vijayakrishna.rg](https://discuss.elastic.co/u/vijayakrishna.rg)\
**Post date:** [December 31, 2019, 7:00pm UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/5 "2019-12-31T19:00:47Z")

</div>

That works, thanks!!!

---

<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 28, 2020, 7:14pm UTC](https://discuss.elastic.co/t/logstash-convert-string-into-milliseconds/213398/6 "2020-01-28T19:14:20Z")

</div>

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