Splunk Search

How to subtract dates from two iso 8601 date fields in order to find the duration?

aahmad
Loves-to-Learn Everything

Hey, I am working on making a dashboard and wanted to know how can I subtract two dates that are in iso 8601 format. 

Please refer to the snippet of json below:

{ "startTime": "2022-04-25T01:02:19.221Z", "endTime": "2022-04-25T01:57:59.417Z"}

Labels (3)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Dates must be converted into epoch (integer) form before they can be subtracted.  Use the strptime function to do that.

| eval st=strptime(startTime, "%Y-%m-%dT%H:%M:%S.%3N%Z"), et=strptime(endTime, "%Y-%m-%dT%H:%M:%S.%3N%Z")
| eval diff = et - st
---
If this reply helps you, Karma would be appreciated.
0 Karma

aahmad
Loves-to-Learn Everything

@richgalloway  I tried your solution. My only problem is that when I do "table diff" in the end. I see empty fields. Do you know how can I fix that?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

A null diff field means the et and/or st fields were null, which probably means the startTime/endTime fields were never extracted.  There are several ways to extract the fields, but I like to use rex.

{ "startTime": "2022-04-25T01:02:19.221Z", "endTime": "2022-04-25T01:57:59.417Z"}

| rex "startTime\\\":\\\"(?<startTime>[^\\\"]+)\\\",\\\"endTime\\\":\\\"(?<endTime>[^\\\"]+))"
| eval st=strptime(startTime, "%Y-%m-%dT%H:%M:%S.%3N%Z"), et=strptime(endTime, "%Y-%m-%dT%H:%M:%S.%3N%Z")
| eval diff = et - st
---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Splunk Mobile: Your Brand-New Home Screen

Meet Your New Mobile Hub  Hello Splunk Community!  Staying connected to your data—no matter where you are—is ...

Introducing Value Insights (Beta): Understand the Business Impact your organization ...

Real progress on your strategic priorities starts with knowing the business outcomes your teams are delivering ...

Enterprise Security (ES) Essentials 8.3 is Now GA — Smarter Detections, Faster ...

As of today, Enterprise Security (ES) Essentials 8.3 is now generally available, helping SOC teams simplify ...