Splunk Search

How to calculate days between 2 dates?

splunkuser320
Path Finder

Hi, Not sure what the issue is. I got the solution from the other answers, but it's not working for me.

I am getting data from splunk date picker and trying to calculate the number of days. 

|addinfo | eval min=info_min_time, max=info_max_time
| eval earliest =strftime(min,"%Y-%m-%d %H:%M:%S")
| eval latest=strftime(max,"%Y-%m-%d %H:%M:%S")
| eval duration = round((latest-earliest)/86400)
|table latest, earliest, duration

 

Thanks

Labels (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The main problem is latest and earliest are strings (the output of strftime()) so it makes no sense to substract them.  The calculation of duration, therefore, results in NULL.  The fix is to use min and max to compute duration.

Also, addinfo is not a generating command so it cannot be the first command in a query.

| makeresults 
| addinfo 
| eval min=info_min_time+10, max=info_max_time 
| eval earliest =strftime(min,"%Y-%m-%d %H:%M:%S") 
| eval latest=strftime(max,"%Y-%m-%d %H:%M:%S") 
| eval duration = round((max-min)/86400) 
| table latest, earliest, duration

 

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...