# How do I subtract two timestamps in VisualBuilder

**URL:** https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277
**Category:** Kibana
**Created:** [March 22, 2018, 10:30pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277 "2018-03-22T22:30:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![iamthealex](https://avatars.discourse-cdn.com/v4/letter/i/e9c0ed/32.png) [@iamthealex](https://discuss.elastic.co/u/iamthealex)
#### Post date: [March 22, 2018, 10:30pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/1 "2018-03-22T22:30:21Z")

</div>

In my JSON I have two fields of type date: @timestamp and ls-timestamp

Let's say that i have the following in my JSON document in ES:

@timestamp in Kibana Discover mode prints this string: March 22nd 2018, 21:54:08.794  
ls-timestamp in Kibana Discover mode prints this string: March 22nd 2018, 21:54:08.886

My goal is to compute a lag from when the application generated the log line (converted to @timestamp) and when LogStash actually processed the log-line (ls-timestamp).

A subtraction given the data above would give me 992 milliseconds or .992 seconds.

I tried to do a calculation, but you have to have agg to calculate on.  
Then I tried to get a series with max of @timestamp, but date is not a number type -- so no max.  
And even if I could use max on @timestamp, calculation would also requires @timestamp to be number AFAICT.

Any pointers are much appreciated.

---

<div class="post-metadata">

### Author: ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)
#### Post date: [March 23, 2018, 2:43pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/2 "2018-03-23T14:43:06Z")

</div>

Hi @iamthealex,

I'm thinking you can make a scripted field to convert the date to a number, then use that in the Visual Builder.

See this post for more information [[scripted field] convert String date to timestamp](https://discuss.elastic.co/t/scripted-field-convert-string-date-to-timestamp/94885)

---

<div class="post-metadata">

### Author: ![iamthealex](https://avatars.discourse-cdn.com/v4/letter/i/e9c0ed/32.png) [@iamthealex](https://discuss.elastic.co/u/iamthealex)
#### Post date: [March 23, 2018, 9:59pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/3 "2018-03-23T21:59:17Z")

</div>

This for responding Chris, but converting date to number is exactly what I need and don't know how to do.  
I looked at the link you posted, and it talks about converting string to Date, but I don't see how to convert date to number.

---

<div class="post-metadata">

### Author: ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)
#### Post date: [March 26, 2018, 3:27pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/4 "2018-03-26T15:27:33Z")

</div>

Hi,

[https://www.elastic.co/guide/en/elasticsearch/reference/5.0/modules-scripting-expression.html#\_date\_field\_api](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/modules-scripting-expression.html#_date_field_api) lists `doc['field_name'].date.secondOfDay` as something you can do. You can convert both to that measurement and compare.

---

<div class="post-metadata">

### Author: ![iamthealex](https://avatars.discourse-cdn.com/v4/letter/i/e9c0ed/32.png) [@iamthealex](https://discuss.elastic.co/u/iamthealex)
#### Post date: [March 26, 2018, 3:48pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/5 "2018-03-26T15:48:20Z")

</div>

Okay, I guess I can do that and if I get a negative number ( because the first timestamp is end of day N and second is beginning of day N+1), then I can "fix it".

doc['field\_name'].date.epochMillis would be a _lot_ easier though.

Thank you.

---

<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: [April 23, 2018, 3:48pm UTC](https://discuss.elastic.co/t/how-do-i-subtract-two-timestamps-in-visualbuilder/125277/6 "2018-04-23T15:48:48Z")

</div>

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