# How to calculate timediff of two fields in the same event

**URL:** https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417
**Category:** Logstash
**Created:** [November 5, 2017, 11:09am UTC](https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417 "2017-11-05T11:09:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ganesh\_Venkataraman](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Ganesh\_Venkataraman](https://discuss.elastic.co/u/Ganesh_Venkataraman)
#### Post date: [November 5, 2017, 11:09am UTC](https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417/1 "2017-11-05T11:09:02Z")

</div>

LOGSTASH - I would like to calculate time taken in seconds for two timestamps in the same event and store it in a different field say "e2e\_latency\_in\_seconds".

The timestamps are in different format. I have successfully normalized them into one format and stored into elasticsearch however I am unable to create a new field using logstash plugin. Please help. I don't think I can use the elapsed plugin as it is for two different events but my case is just one event.

```
date{
	match => ["tds_audittimestamp", "UNIX_MS"]
	target => "tds_audittimestamp"
}

date{
	match => ["trade_insertion_at_source", "yyyy/MM/dd HH:mm:ss.SSS"]
	timezone => "UTC"
	target => "trade_insertion_at_source"
	}

```

I have calculated using kibana scripted field but I want to achieve this in logstash as I cannot used scripted fields in Grafana.

![screenshot](https://us1.discourse-cdn.com/elastic/original/3X/5/9/59377160a31298b71af7468556c3df8c202d8b8c.jpg)

---

<div class="post-metadata">

### Author: ![novoselrok](https://avatars.discourse-cdn.com/v4/letter/n/b3f665/32.png) [@novoselrok](https://discuss.elastic.co/u/novoselrok)
#### Post date: [November 5, 2017, 11:29am UTC](https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417/2 "2017-11-05T11:29:15Z")

</div>

Try using the Ruby filter plugin:  
[https://www.elastic.co/guide/en/logstash/current/plugins-filters-ruby.html](https://www.elastic.co/guide/en/logstash/current/plugins-filters-ruby.html)

---

<div class="post-metadata">

### Author: ![Ganesh\_Venkataraman](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Ganesh\_Venkataraman](https://discuss.elastic.co/u/Ganesh_Venkataraman)
#### Post date: [November 6, 2017, 7:58am UTC](https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417/3 "2017-11-06T07:58:14Z")

</div>

I don't know ruby so found it very difficult. I have used below post to find the solution.

> [@Logstash Ruby filter to subtract difference between two timestamps in single event](https://discuss.elastic.co/t/logstash-ruby-filter-to-subtract-difference-between-two-timestamps-in-single-event/32580/4):
>
> I think you could just do something like: ruby { init =\> "require 'time'" code =\> "event['time\_difference'] = event['received\_at'] - event['@timestamp'];" }

---

<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: [December 4, 2017, 7:58am UTC](https://discuss.elastic.co/t/how-to-calculate-timediff-of-two-fields-in-the-same-event/106417/4 "2017-12-04T07:58:15Z")

</div>

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