# Convert timestamp to epoctime with milisecond

**URL:** https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899
**Category:** Logstash
**Created:** [April 5, 2018, 10:52am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899 "2018-04-05T10:52:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Masahiro\_Shimizu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/masahiro_shimizu/32/29613_2.png) [@Masahiro\_Shimizu](https://discuss.elastic.co/u/Masahiro_Shimizu)
#### Post date: [April 5, 2018, 10:52am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899/1 "2018-04-05T10:52:15Z")

</div>

Hi , this is my first question.

I would like convert @timestamp to epoch-time with millisecond  
Because I want millisecond not second , I try check the latency through the messaging queue.

Here is my code,

* * *

ruby {  
code =\> "event.set('epoc', event.get('@timestamp').to\_i)"  
}

* * *

Is there the other method ? ( to\_i method convert to second)

---

<div class="post-metadata">

### Author: ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)
#### Post date: [April 5, 2018, 11:07am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899/2 "2018-04-05T11:07:00Z")

</div>

You could convert to float, multiply by 1000 and convert the result to int. (But maybe there is a more elegant way.)

```
ruby {
    code => "event.set('epoc', (event.get('@timestamp').to_f*1000).to_i)"
  }
```

---

<div class="post-metadata">

### Author: ![Masahiro\_Shimizu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/masahiro_shimizu/32/29613_2.png) [@Masahiro\_Shimizu](https://discuss.elastic.co/u/Masahiro_Shimizu)
#### Post date: [April 6, 2018, 3:03am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899/3 "2018-04-06T03:03:18Z")

</div>

Thank you quick response. Jenni.  
I didn't know to\_f method.

Though I try it , millisecond part is all zero (like below)

```
  a)my old code (to_i method)
   - code  
               code => "event.set('epoc', event.get('@timestamp').to_i)"
   - output
               "epoc" => 1522983522,
  b)new code (you suggested)
   - code  
               code => "event.set('epoc', (event.get('@timestamp').to_f * 1000).to_i)"
   - output
                "epoc" => 1522983522000,

```

I want accurate value of milli second ( "000" of 21522983522000").  
Could you have more idea ?

---

<div class="post-metadata">

### Author: ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)
#### Post date: [April 6, 2018, 8:07am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899/4 "2018-04-06T08:07:37Z")

</div>

Mh. That's strange. I had tried that before posting and it seemed to work for me:

"@timestamp" =\> 2018-04-06T07:59:21.130Z,  
"epoc" =\> 1523001561130

"@timestamp" =\> 2018-04-06T08:01:36.846Z,  
"epoc" =\> 1523001696846

Is there a chance that you are losing that level of precision one step earlier when you are creating the timestamp? What does the original timestamp look like in the rubydebug output?

---

<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: [May 4, 2018, 8:07am UTC](https://discuss.elastic.co/t/convert-timestamp-to-epoctime-with-milisecond/126899/5 "2018-05-04T08:07:37Z")

</div>

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