# If statement to check if difference between two values is greater than number

**URL:** <https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432>\
**Category:** Logstash\
**Created:** [March 28, 2019, 9:09pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432 "2019-03-28T21:09:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greengeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/greengeek/32/43035_2.png) [@greengeek](https://discuss.elastic.co/u/greengeek)\
**Post date:** [March 28, 2019, 9:09pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/1 "2019-03-28T21:09:41Z")

</div>

I have two fields that are epoch timestamps. I want to take the first field and subtract the second field. If the results are larger than a certain number, I want to write a value into a new field.

Basically an IF statement something like IF (a - b) \> 2592000 #the equivalent to 30 days epoch time  
Then write value some text to field.

This is the code I have. I verified that I am getting two values set as integers for the times, but I don't think I have the IF statement correct and/or the THEN write statement.

```
ruby {    
	code => "
		event.set('date_plugin_mod_epoch', event.get('plugin_modification_date').to_i)  
		event.set('date_add_epoch', event.get('@timestamp').to_i)
		if (event.get('date_add_epoch') - event.get('date_plugin_mod_epoch')) > 2592000
			event.set('past_due', 'Past Due')
		end"  
} 	

```

Any suggestions please? 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:** [March 29, 2019, 12:53pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/2 "2019-03-29T12:53:24Z")

</div>

That code works just fine for me provided that plugin\_modification\_date is a Logstash::Timestamp. Although I would write it as

```
        code => "
            date_plugin_mod_epoch = event.get('plugin_modification_date').to_i
            date_add_epoch = event.get('@timestamp').to_i
            if date_add_epoch - date_plugin_mod_epoch > 2592000
                event.set('past_due', 'Past Due')
            end
        "

```

```auto
{
    "plugin_modification_date" => 2019-02-27T16:23:12.000Z,
                  "@timestamp" => 2019-03-29T12:50:56.230Z,
                         "foo" => "2019/02/27 11:23:12", [...]
}
{
    "plugin_modification_date" => 2019-02-26T16:23:12.000Z,
                  "@timestamp" => 2019-03-29T12:51:02.205Z,
                         "foo" => "2019/02/26 11:23:12",
                    "past_due" => "Past Due", [...]
}

```

What exactly do your two "epoch timestamps" look like.

---

<div class="post-metadata">

**Author:** ![greengeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/greengeek/32/43035_2.png) [@greengeek](https://discuss.elastic.co/u/greengeek)\
**Post date:** [March 29, 2019, 3:00pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/3 "2019-03-29T15:00:29Z")

</div>

Thank you for your response. My epoch timestamps look like this, 1553871497 and 1518533837 as an example. They are in unix epoch time 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:** [March 29, 2019, 4:19pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/4 "2019-03-29T16:19:42Z")

</div>

OK, your code works just fine with that format too.

---

<div class="post-metadata">

**Author:** ![greengeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/greengeek/32/43035_2.png) [@greengeek](https://discuss.elastic.co/u/greengeek)\
**Post date:** [March 29, 2019, 5:00pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/5 "2019-03-29T17:00:31Z")

</div>

Strange that I cannot get my code to work. I am going to try your example as it looks far better. I like how you set the values to variables, instead of how i set them to fields then had to remove them after.

---

<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:** [March 29, 2019, 5:06pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/6 "2019-03-29T17:06:03Z")

</div>

I suggest you calculate the difference and save that to a field. Maybe it will make you see something that you are missing.

---

<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:** [April 26, 2019, 5:06pm UTC](https://discuss.elastic.co/t/if-statement-to-check-if-difference-between-two-values-is-greater-than-number/174432/7 "2019-04-26T17:06:08Z")

</div>

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