# Logstash : How to compute endDate from startDate + duration

**URL:** <https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082>\
**Category:** Logstash\
**Created:** [January 10, 2017, 12:36pm UTC](https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082 "2017-01-10T12:36:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnico](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cnico](https://discuss.elastic.co/u/cnico)\
**Post date:** [January 10, 2017, 12:36pm UTC](https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082/1 "2017-01-10T12:36:07Z")

</div>

Hello,

I have an event in which its data is composed of a start dateTime and of duration in seconds.  
I would like to compute via logstash, the end dateTime with the problem of day change if duration high.

Is possible to do that with some ruby code and how ?

Thank you,

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 12, 2017, 6:03am UTC](https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082/2 "2017-01-12T06:03:48Z")

</div>

This has been asked a few times, but yes, do it in ruby.  
Have a search in this category and you will find some ideas 🙂

---

<div class="post-metadata">

**Author:** ![cnico](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cnico](https://discuss.elastic.co/u/cnico)\
**Post date:** [January 16, 2017, 12:36pm UTC](https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082/3 "2017-01-16T12:36:22Z")

</div>

I did not find the exact use case in the forum, but searching on it and with some tries, here is my solution :

```
    date {
        match => ["startTime", "dd/MM/YYYY HH:mm:ss.SSS"]
        timezone => "Europe/Paris"
        target => "startTime"
    }
    
    ruby {
        init => "require 'time'"
        code => "
            durationInSeconds = event.get('durationMs').to_f / 1000;
            #Addition of duration must be in seconds here with a float number eventually for ms
            timeVal = Time.iso8601(event.get('startTime').to_s) + durationInSeconds 
            #Converts to logstash object of timestamp :
            event.set('startTime', LogStash::Timestamp.new(timeVal))
        "
    }

```

It works also with logstash 2.3 with the correct event manipulation as an array instead of getter and setter.

---

<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:** [February 13, 2017, 12:36pm UTC](https://discuss.elastic.co/t/logstash-how-to-compute-enddate-from-startdate-duration/71082/4 "2017-02-13T12:36:23Z")

</div>

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