# Calculate time interval between 2 timestamps records

**URL:** <https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825>\
**Category:** DQL\
**Tags:** dql, timeframe, dashboards, logs\
**Created:** [January 25, 2024, 10:22am UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825 "2024-01-25T10:22:16Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 10:22am UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/1 "2024-01-25T10:22:16Z")

</div>

![AlexisGND_0-1706177661722.png](https://global.discourse-cdn.com/dynatrace/original/3X/c/d/cd6afa788028ee1412967e15c6bd1eb829c64054.png)

Hi, I ingested customized logs into Dynatrace.

I have 2 columns named “bmx.hip.execution.endtime” and&nbsp;“bmx.hip.execution.starttime” with are timestamps in iso format.

Is there any way to calculate the time interval between the two of them in a separate column that I created “timediff” ?

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 10:24am UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/2 "2024-01-25T10:24:08Z")

</div>

Hi&nbsp;[@AlexisGND](https://community.dynatrace.com/user/68059)&nbsp;,

you may try this :

```markup
| fieldsAdd timeDiff = endtime - startime
| fields timeDiff

```

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 10:50am UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/3 "2024-01-25T10:50:55Z")

</div>

Hi&nbsp;[@gbaudart](https://community.dynatrace.com/user/64996),&nbsp;thanks for your reply.

I tried this DQL command :

 ![AlexisGND_2-1706179239363.png](https://global.discourse-cdn.com/dynatrace/original/3X/2/a/2aa44aa115475f368d9a8b8d53dce1fc0f6fe375.png)

I just changed the name of the column in english (and not in french).

But “timeDiff” is still on “null”, does that means that timeDiff is not able to calculate time interval with 2 iso formats ?

Result :

 ![AlexisGND_1-1706179220337.png](https://global.discourse-cdn.com/dynatrace/original/3X/b/1/b1911ecd41b39d9f2a5821c41928004c01a11d3a.png)

**If it’s not possible to calculate the time interval, i tried an alternative way (only if the DQL above is not the solution)** :

(i tried an alernative way to do substings with the timestamp in order to get and associates numbers with Year, month etc)  
It’s done like this :

 ![AlexisGND_4-1706179692560.png](https://global.discourse-cdn.com/dynatrace/original/3X/c/5/c54cc7893838fba1bf08f30a67b21e6a29591643.png)

But i get an error that shows it’s not possible to calculate between numbers (or i don’t understand the error message).

 ![AlexisGND_3-1706179656623.png](https://global.discourse-cdn.com/dynatrace/original/2X/d/dcbffac1a57ba176e5375857e4db2e980cf973c7.png)

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 12:12pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/4 "2024-01-25T12:12:11Z")

</div>

I’m doing exactly the same thing in my environment with the DQL request I sent.  
There must be something to fix.  
I’ll think about it.

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 12:21pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/5 "2024-01-25T12:21:29Z")

</div>

Try this :

```markup
| fieldsAdd StartTimeModif = formatTimestamp(StartTime,format:"dd MMM YYYY HH:mm:ss"), EndTimeModif = formatTimestamp(EndTime,format:"dd MMM YYYY HH:mm:ss"), DiffTime = EndTimeModif - StartTimeModif
| fields DiffTime

```

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 12:51pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/6 "2024-01-25T12:51:34Z")

</div>

![AlexisGND_0-1706186151819.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/9640d5c91e5534cd004c9abf0c79acf9a7068330.png)

DiffTime is still on “null”  
Even if i change to End\_execution and Start\_execution

![AlexisGND_1-1706186193419.png](https://global.discourse-cdn.com/dynatrace/original/1X/9813019c242ff849631ffa9a8e7510f25222a08b.png)

Even when i display everything, I have null on StartTime and EndTime :

 ![AlexisGND_2-1706187082909.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/0/60f23dcaccc46e6847d8b9082b3c6ff01075c278.png)

I am currently looking at the time functions documentation&nbsp;[Time functions - Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/time-functions#duration)

I will let you know if i find anything

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 1:11pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/7 "2024-01-25T13:11:11Z")

</div>

your variable aren’t good.

Try this :

```markup
| fieldsAdd StartTimeModif = formatTimestamp(Start_execution,format:"dd MMM YYYY HH:mm:ss"), EndTimeModif = formatTimestamp(End_execution,format:"dd MMM YYYY HH:mm:ss"), DiffTime = EndTimeModif - StartTimeModif
| fields DiffTime

```

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 1:30pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/8 "2024-01-25T13:30:19Z")

</div>

![AlexisGND_0-1706189110413.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/b/9bd3bc792b626947cd01a2c9acf2ed8b0f7e6d22.png)

I changed the time format because I saw in the line :

```auto
format:"dd MMM YYYY HH:mm:ss"

```

Now Start and End have time but DiffTime has nothing

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 1:52pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/9 "2024-01-25T13:52:40Z")

</div>

May you paste your entire query pls ?

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 2:17pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/10 "2024-01-25T14:17:42Z")

</div>

![AlexisGND_0-1706192175103.png](https://global.discourse-cdn.com/dynatrace/original/3X/e/6/e66d1f730cd11ce709576c515203f4ecfb9d0e53.png)

```auto
// get logs
fetch logs
// filter timestamp descending
| sort timestamp desc

// rename columns
| fieldsRename End_execution = bmx.hip.execution.endtime
| fieldsRename Start_execution = bmx.hip.execution.starttime
| fieldsRename HostName = bmx.hip.host.name

// filter host names with Datastage
| filter HostName == "Datastage" or HostName == "DATASTAGE" or HostName == "datastage"

// display columns
| fieldsAdd StartTimeModif = formatTimestamp(Start_execution,format:"dd MMM YYYY HH:mm:ss"), EndTimeModif = formatTimestamp(End_execution,format:"dd MMM YYYY HH:mm:ss"), DiffTime = EndTimeModif - StartTimeModif
| fields DiffTime, Start_execution, StartTimeModif, End_execution, EndTimeModif

```

the “formatted” field was just a test.  
But my guess is the incorrect format of Start\_execution and End\_execution

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 2:48pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/11 "2024-01-25T14:48:10Z")

</div>

By doing a test, when I do “formatTimestamp”, the default timestamp is correct but even when the Endtime is with the same format as the timestamp, still got a null.  
I really don’t get it… 😞

![AlexisGND_0-1706194067044.png](https://global.discourse-cdn.com/dynatrace/original/3X/2/4/24adf8cdf7fca0661c8fecfe2de6cbb6f39e66ab.png)

```auto
fetch logs
| sort timestamp desc
| filter bmx.hip.host.name == "Datastage"

| fieldsAdd formatted = formatTimestamp(timestamp, format:"MM-dd-YYYY")
| fieldsAdd formatted2 = formatTimestamp(bmx.hip.execution.endtime, format:"MM-dd-YYYY")

| fields timestamp, formatted, bmx.hip.execution.endtime, formatted2

```

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 3:10pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/12 "2024-01-25T15:10:13Z")

</div>

I will try to test with your dataset

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [January 25, 2024, 3:12pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/13 "2024-01-25T15:12:08Z")

</div>

I suspect that ingested fields are just Strings. To do any arithmetics they need to be converted to timestamps. _ **toTimestamp** _ function should catch iso format. Please try query based on this example:

```markup
data record(ts="2024-01-22 15:30:01.000000", te="2024-01-22 15:30:01.500000")
| fieldsAdd pts=toTimestamp(ts), pte=toTimestamp(te)
| fieldsAdd d=pte-pts

```

 ![krzysztof_hoja_0-1706195439880.png](https://global.discourse-cdn.com/dynatrace/original/3X/f/8/f86e10c466b5cee0eb5a0b8db84015f1b9524227.png)

---

<div class="post-metadata">

**Author:** ![AlexisGND](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AlexisGND](https://community.dynatrace.com/u/AlexisGND)\
**Post date:** [January 25, 2024, 3:37pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/14 "2024-01-25T15:37:07Z")

</div>

So I finally arrived at the end result 🙂

**What I did ?**  
The start time (as bmx.hip.execution.starttime) and end time (as bmx.hip.execution.endtime) were converted to timestamp formats with this lines :

```auto
// convert start and end times to timestamp format
| fieldsAdd bmx.hip.execution.endtime = toTimestamp(bmx.hip.execution.endtime),
bmx.hip.execution.starttime = toTimestamp(bmx.hip.execution.starttime)

```

And then I added a new field to substract the endtime with the starttime.

```auto
| fieldsAdd bmx.hip.execution.duration = bmx.hip.execution.endtime - bmx.hip.execution.starttime

```

_ **Final dql :** _

```auto
// get logs
fetch logs
// filter timestamp descending
| sort timestamp desc

// filter host names with Datastage
| filter bmx.hip.host.name == "Datastage" or bmx.hip.host.name == "DATASTAGE" or bmx.hip.host.name == "datastage"
| filter loglevel == "NOTICE" or loglevel == "NONE" or loglevel == "DEBUG"

// convert start and end times to timestamp format
| fieldsAdd bmx.hip.execution.endtime = toTimestamp(bmx.hip.execution.endtime),
bmx.hip.execution.starttime = toTimestamp(bmx.hip.execution.starttime)
| fieldsAdd bmx.hip.execution.duration = bmx.hip.execution.endtime - bmx.hip.execution.starttime

// display columns
| fields content, bmx.hip.execution.starttime, bmx.hip.execution.endtime, bmx.hip.execution.duration, bmx.hip.execution.status

// rename columns
| fieldsRename End_execution = bmx.hip.execution.endtime
| fieldsRename Start_execution = bmx.hip.execution.starttime

```

_ **Final visual result :** _

 ![AlexisGND_0-1706197019925.png](https://global.discourse-cdn.com/dynatrace/original/3X/1/a/1a7455761fc6b56dd6debff6a79a2ce0a4492056.png)

Thanks [@gbaudart](https://community.dynatrace.com/user/64996) and [@krzysztof\_hoja](https://community.dynatrace.com/user/25373) for your help !

---

<div class="post-metadata">

**Author:** ![gbaudart](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gbaudart](https://community.dynatrace.com/u/gbaudart)\
**Post date:** [January 25, 2024, 3:45pm UTC](https://community.dynatrace.com/t/calculate-time-interval-between-2-timestamps-records/43825/15 "2024-01-25T15:45:58Z")

</div>

If you just try this :

```markup
fetch logs
| sort timestamp desc
| filter bmx.hip.host.name == "Datastage"

| fields toTimestamp(bmx.hip.execution.endtime), toTimestamp(bmx.hip.execution.starttime)

```

Do you also get “null” for the 2 results?
