# Time Diff Calculation in ruby not working

**URL:** https://discuss.elastic.co/t/time-diff-calculation-in-ruby-not-working/167775
**Category:** Logstash
**Created:** [February 10, 2019, 11:23pm UTC](https://discuss.elastic.co/t/time-diff-calculation-in-ruby-not-working/167775 "2019-02-10T23:23:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ssasporta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ssasporta/32/13695_2.png) [@ssasporta](https://discuss.elastic.co/u/ssasporta)
#### Post date: [February 10, 2019, 11:23pm UTC](https://discuss.elastic.co/t/time-diff-calculation-in-ruby-not-working/167775/1 "2019-02-10T23:23:27Z")

</div>

Hi,

**What is wrong in the following filter lines that I am not able to find the TimeDiff field in the output?**

```
     date {
           match => ["Criado em", "dd/MM/yyyy HH:mm:ss", "ISO8601"]
           target => "CreateDate"
     }
     date {
           match => ["Data de Fechamento", "dd/MM/yyyy HH:mm:ss", "ISO8601"]
           target => "ClosedDate"
     }
     if [ClosedDate] =~ /.+/ {
                              ruby {
                                    init => "require 'time'"
                                    code => "
                                             starttime = Time.iso8601(event.get('CreateDate').to_s).to_f;
                                             endtime = Time.iso8601(event.get('ClosedDate').to_s).to_f;
                                             event.set('TimeDiff', endtime - starttime);
                                    "
                              }
     }

```

**I can see in the Kibana, in the created index:**

ClosedDate September 22nd 2018, 02:00:00.000

CreateDate September 14th 2018, 20:07:39.000

Regards,  
Sharon.

---

<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: [February 10, 2019, 11:37pm UTC](https://discuss.elastic.co/t/time-diff-calculation-in-ruby-not-working/167775/2 "2019-02-10T23:37:53Z")

</div>

> [@ssasporta](#):
>
> if [ClosedDate] =~ /.+/ {

This is a regexp match on a Logstash::TimeStamp object. It does not do what you want, so the ruby filter is not run. You might want

```
if [ClosedDate] {

```

---

<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: [March 10, 2019, 11:37pm UTC](https://discuss.elastic.co/t/time-diff-calculation-in-ruby-not-working/167775/3 "2019-03-10T23:37:57Z")

</div>

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