# How change the format of duration between two dates in dateTime?

**URL:** <https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488>\
**Category:** Logstash\
**Created:** [April 10, 2020, 11:28am UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488 "2020-04-10T11:28:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)\
**Post date:** [April 10, 2020, 11:28am UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488/1 "2020-04-10T11:28:49Z")

</div>

I calculate the difference between two dates with this code.  
ruby{  
code =\> "event.set('Duration', (event.get('Release\_date') - event.get('Date\_of\_entry')))"  
}

the output that i have : field duration with type number like this: 18000  
how can i get the difference in time HH:mm:ss ??  
please any ideas??

---

<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:** [April 10, 2020, 1:54pm UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488/2 "2020-04-10T13:54:02Z")

</div>

You could do that using a ruby filter. See [this](https://stackoverflow.com/questions/1679266/can-ruby-print-out-time-difference-duration-readily) post for some ideas.

---

<div class="post-metadata">

**Author:** ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)\
**Post date:** [April 10, 2020, 2:47pm UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488/3 "2020-04-10T14:47:45Z")

</div>

Sorry, Maybe this question will be stupid but really i don't know how to use or add this code to my logstash file config and i must calculate the duration between two events (type date) ??can you help me please?

time\_difference = current\_time - old\_time

def seconds\_fraction\_to\_time(time\_difference)  
days = hours = mins = 0  
mins = (seconds / 60).to\_i  
seconds = (seconds % 60 ).to\_i  
hours = (mins / 60).to\_i  
mins = (mins % 60).to\_i  
days = (hours / 24).to\_i  
hours = (hours % 24).to\_i  
return [days,hours,mins,seconds]  
end

then you can print it out what ever way you wish,

if(days \> 0)  
return "#{days} Days #{hours} Hours"  
else  
return "#{hours} Hours #{mins} Minutes"  
end

---

<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:** [April 10, 2020, 3:50pm UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488/4 "2020-04-10T15:50:18Z")

</div>

The [documentation](https://www.elastic.co/guide/en/logstash/current/plugins-filters-ruby.html) shows how to call code in a ruby filter.

---

<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:** [May 8, 2020, 3:50pm UTC](https://discuss.elastic.co/t/how-change-the-format-of-duration-between-two-dates-in-datetime/227488/5 "2020-05-08T15:50:20Z")

</div>

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