# How to calculate the timestamp difference between two actions?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310>\
**Category:** Kibana\
**Created:** [May 25, 2018, 11:21am UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310 "2018-05-25T11:21:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maedamaeda](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@maedamaeda](https://discuss.elastic.co/u/maedamaeda)\
**Post date:** [May 25, 2018, 11:21am UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310/1 "2018-05-25T11:21:29Z")

</div>

Hello,  
I'd like to calculate the timestamp difference between two actions.  
But I can't find a way to do it.

Log example is below.

| @timestamp | action |
| --- | --- |
| May 20th 2018, 15:41:32.111 | login |
| May 21th 2018, 10:39:01.761 | logout |

Please guide.

Thank you.  
maedamaeda

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [May 25, 2018, 5:37pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310/2 "2018-05-25T17:37:59Z")

</div>

What do your Elasticsearch indices and docs look like? Is the log example data in one document, or multiple documents?

---

<div class="post-metadata">

**Author:** ![maedamaeda](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@maedamaeda](https://discuss.elastic.co/u/maedamaeda)\
**Post date:** [May 28, 2018, 12:12am UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310/3 "2018-05-28T00:12:54Z")

</div>

My Elasticsearch indices web application logs.  
The logs are in multiple documents.

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [June 8, 2018, 5:47pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310/4 "2018-06-08T17:47:16Z")

</div>

I recommend restructuring the data sent to Elasticsearch so that each document looks something like this:

```auto
{
  "session_id": 123456,
  "login_time": "May 20th 2018, 15:41:32.111",
  "logout_time": "May 21th 2018, 10:39:01.761",
}

```

Then, you can add a [Scripted field in Kibana](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html) to calculate the difference between `login_time` and `logout_time`.

You can read more about [modeling data for Elasticsearch here](https://www.elastic.co/guide/en/elasticsearch/guide/current/modeling-your-data.html).

---

<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:** [July 6, 2018, 5:48pm UTC](https://discuss.elastic.co/t/how-to-calculate-the-timestamp-difference-between-two-actions/133310/5 "2018-07-06T17:48:41Z")

</div>

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