# Time difference between two fields in a csv using ruby plugin

**URL:** https://discuss.elastic.co/t/time-difference-between-two-fields-in-a-csv-using-ruby-plugin/122115
**Category:** Logstash
**Created:** [March 1, 2018, 3:49pm UTC](https://discuss.elastic.co/t/time-difference-between-two-fields-in-a-csv-using-ruby-plugin/122115 "2018-03-01T15:49:23Z")
**Posts on this page:** 1
**Showing post:** 2

<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 1, 2018, 4:20pm UTC](https://discuss.elastic.co/t/time-difference-between-two-fields-in-a-csv-using-ruby-plugin/122115/2 "2018-03-01T16:20:11Z")

</div>

As the message says, you should be using event.get to access fields, and event.set to add them. For example, this code calculates the differences (in seconds) between two times. It does something slightly different to what you want, and comes from an aggregate filter, so is structured differently, but accesses and sets fields, so should give you some idea of what to do.

```auto
  code => "
    require 'time';
    starttime = Time.iso8601(map['starttime'].to_s).to_f;
    endtime = Time.iso8601(event.get('@timestamp').to_s).to_f;
    event.set('overall_timetaken', endtime - starttime);
  "

```

---

_[View the full topic](https://discuss.elastic.co/t/time-difference-between-two-fields-in-a-csv-using-ruby-plugin/122115)._
