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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...