# Calculate time difference between two dates ( start date and end date of an log file )

**URL:** <https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916>\
**Category:** Logstash\
**Created:** [April 29, 2019, 11:30am UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916 "2019-04-29T11:30:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Badem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badem/32/45225_2.png) [@Badem](https://discuss.elastic.co/u/Badem)\
**Post date:** [April 29, 2019, 11:30am UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916/1 "2019-04-29T11:30:55Z")

</div>

Hello, everyone.  
I'm trying without success to calculate the difference between the start date and the end date of my log file in order to get the time duration ( in second ) of an script.

**Here is my input :**

```
17/04/2019 13:30:00 STARTED INFO :START OF THE LOG FILE
17/04/2019 13:35:00 ENDED INFO :END OF THE LOG FILE

```

**What I want in output is :**

```
{
start_date : 17/04/2019 13:30:37,
end_date : 17/04/2019 17:13:02,
log_duration : 300.00
}

```

**And here is my conf file :**

```
input {
  file {
    path => ["C:/Users/log.conf"]
    start_position => "beginning"
    sincedb_path => "NUL"
    codec => plain {
                   charset => "ISO-8859-1"
                  }
    
  }
}
filter {
  
  grok{
    
    patterns_dir => ["../patterns"]
    match =>{"message" => "%{DATE:date_date}%{SPACE}%{TIME:time}%{STATUS:state}%{SPACE}%{LOGLEVEL:level}%{SPACE}\:%{GREEDYDATA:msg}"}

  } 

if [state] =="STARTED" {

 mutate{
   add_field =>{
     "start_date" => "%{date_date} %{time}"
   }
  
 }
  date{
      match => ["start_date","dd/MM/yyyy HH:mm:ss"]
      target => "start_date"
    }
  
}
  
if [state] == "ENDED" {

    mutate{
      add_field => {
      "end_date" => "%{date_date} %{time}"
      }

    }
    date{
      match => ["end_date","dd/MM/yyyy HH:mm:ss"]
      target => "end_date"
    }
 

}   

ruby {
      
      code => 'event.set("duration_hrs", (event.get("end_date")- event.get("start_date")) ) rescue nil'
    }

}
mutate {
    remove_field => ["date_date", "time","state","level","msg"]
  }

output {
if "_grokparsefailure" not in [tags] {
  
    stdout { codec => rubydebug { metadata => true }}
}

}

```

Thank you in advance for helping me 👍

---

<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 29, 2019, 12:01pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916/2 "2019-04-29T12:01:53Z")

</div>

Take a look at [example 1](https://www.elastic.co/guide/en/logstash/current/plugins-filters-aggregate.html#plugins-filters-aggregate-example1) in the documentation of the aggregate filter.

---

<div class="post-metadata">

**Author:** ![Badem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badem/32/45225_2.png) [@Badem](https://discuss.elastic.co/u/Badem)\
**Post date:** [April 29, 2019, 12:58pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916/3 "2019-04-29T12:58:45Z")

</div>

thank you #Badger, i'll take a look at that

---

<div class="post-metadata">

**Author:** ![Badem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badem/32/45225_2.png) [@Badem](https://discuss.elastic.co/u/Badem)\
**Post date:** [April 29, 2019, 1:24pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916/4 "2019-04-29T13:24:56Z")

</div>

It works well, thanks:clap:👏

---

<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 27, 2019, 1:25pm UTC](https://discuss.elastic.co/t/calculate-time-difference-between-two-dates-start-date-and-end-date-of-an-log-file/178916/5 "2019-05-27T13:25:03Z")

</div>

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