# How to calculate elapsed Time between Timestamps in a given Timerange?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449>\
**Category:** Kibana\
**Created:** [September 6, 2021, 12:12pm UTC](https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449 "2021-09-06T12:12:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elastomer](https://avatars.discourse-cdn.com/v4/letter/e/e36b37/32.png) [@Elastomer](https://discuss.elastic.co/u/Elastomer)\
**Post date:** [September 6, 2021, 12:12pm UTC](https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449/1 "2021-09-06T12:12:08Z")

</div>

Hallo fellow Users.

I would like to know how i can get the Time difference from the "First" to the"Last" Document within a specific Time Range.

Question: How much distance did i travel in a specific Time Range.

Situation (Aviation Data) :

Dokument has @Timestamp, Speed, and Altitude, Location

I want to know the covered `Distance = Average Speed * Time interval` (e.g. last 5 hours).

Displayed in the Dashboard and Canvas via a Visualisation would be appreciated

Data is ingested via Filebeat into Elastic Cloud Service.

My Idea:  
(Max.@Timerange - Min.@Timestamp) \* Avergage Speed

but How =)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Marta\_Bondyra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marta_bondyra/32/102122_2.png) [@Marta\_Bondyra](https://discuss.elastic.co/u/Marta_Bondyra)\
**Post date:** [September 7, 2021, 11:25am UTC](https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449/2 "2021-09-07T11:25:27Z")

</div>

Hi there, this is a case for TSVB bucket scripting. Here's the similar (bur reverse) example. In kibana\_sample\_data\_flight we have available `Distance` field so I am gonna calculate average speed per hour:

 ![Screenshot 2021-09-07 at 13.22.28](https://us1.discourse-cdn.com/elastic/original/3X/f/b/fb638693a87ca18e1fb4a90f8faaf86835a47721.png)

In your case the script will be something like `params.averageSpeed * (params._interval/1000/60/60)`  
The params.\_interval is passed as ms so we need to convert it to an hour with `/1000/60/60` or just `3600000`.

---

<div class="post-metadata">

**Author:** ![Elastomer](https://avatars.discourse-cdn.com/v4/letter/e/e36b37/32.png) [@Elastomer](https://discuss.elastic.co/u/Elastomer)\
**Post date:** [September 12, 2021, 5:25pm UTC](https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449/3 "2021-09-12T17:25:24Z")

</div>

Thank you very much @Marta_Bondyra for your Help!  
It was really helpful and i took some time to fiddle with the TSVB hence i reply this late.

I have one more "Problem", that you might have a answer to.

My Data is from a Flight Tracker Website (CSV) and the Airplane in Question is not tracked 100% of the Flight. Most of the Time i get the Takeoff and Landing within the Data but missing on several hours of the Flight.

To get the Total Flighttime i am thinking of somehow extracting the first and last Entry in the CSV and calculate the "Total Time".

My approach would be either:

- Scriped Processor within a Pipeline
- Runtime Field

Your approach is totally working, no doubt, but for my missing Data it gives me trouble.

1h Interval --\> Average Speed = Distance Traveld  
Cumulative Sum of Dinstance remains zero if no metrics are recieved.

 ![grafik](https://us1.discourse-cdn.com/elastic/original/3X/5/7/57fae2d627d062f8f2c87ca8a50c56918f4b03da.png)

Kinds regards

---

<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:** [October 10, 2021, 5:26pm UTC](https://discuss.elastic.co/t/how-to-calculate-elapsed-time-between-timestamps-in-a-given-timerange/283449/4 "2021-10-10T17:26:24Z")

</div>

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