# How to calculate the difference between two web sessions in seconds

**URL:** <https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 20, 2021, 1:17pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624 "2021-09-20T13:17:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marone/32/87145_2.png) [@marone](https://discuss.elastic.co/u/marone)\
**Post date:** [September 20, 2021, 1:17pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624/1 "2021-09-20T13:17:13Z")

</div>

Hey I am trying to understand how this transformation work: [Getting duration by using bucket script](https://www.elastic.co/guide/en/elasticsearch/reference/master/transform-painless-examples.html#painless-bucket-script). Before applying it to my use-case I am trying to know how to get the difference between two sessions in seconds? and the returned value by the example in which unit (months?, days?, seconds?).

and in the example the:

```auto
"time_frame.lte": {
        "max": {
          "field": "timestamp"
        }
      }

```

mean we compare both timestamps and we take the lowest value (because of lte) then we assign it to the field max which correspond to the first timestamp (timestamp1) or the second one (timestamp2) of a web session? as timestamp1 \< timestamp2.

---

<div class="post-metadata">

**Author:** ![Fenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fenix/32/94339_2.png) [@Fenix](https://discuss.elastic.co/u/Fenix)\
**Post date:** [September 20, 2021, 2:51pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624/2 "2021-09-20T14:51:33Z")

</div>

From what I'm seeing in the documentation, this transform performs to top-level aggregations (one for time\_frame.lte, and another for time\_frame.gte).

Once that data is collected, a scripted field calculates the duration where params.max (lte as upper limit) \> params.min (gte as lower limit). The duration needs to be a positive value.

I believe by default, the result diff is in epoch, but can be converted to millis (using getMillis()).

---

<div class="post-metadata">

**Author:** ![marone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marone/32/87145_2.png) [@marone](https://discuss.elastic.co/u/marone)\
**Post date:** [September 21, 2021, 3:57pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624/3 "2021-09-21T15:57:21Z")

</div>

Thank you @Fenix for your reply, after some test I found that the returned value from:

```auto
"min": "time_frame.gte.value",
"max": "time_frame.lte.value"

```

are in miliseconds, I used an online tool ([Calculate Date/Time Difference](https://www.epochconverter.com/date-difference)) to compare the data I have, to get the output in seconds needs only: `"script": "(params.max - params.min)/1000"`

---

<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 19, 2021, 3:57pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-difference-between-two-web-sessions-in-seconds/284624/4 "2021-10-19T15:57:47Z")

</div>

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