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!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...