Splunk Search

How to get the difference between 2 date fields?

ajdyer2000
Path Finder

Hi All

thank you all so much for helping me. this is a great forum to learn.

I have 2 date fields and I'd like to get the difference in days and if they are over certain thresholds <30 ,>30, >60, >120

For Example

Current-Date Open-Date Diff Days  Metric
4/25/2022 4/23/2022 2 <30
4/25/2022 3/15/2022 41 >30
4/25/2022 2/15/2022 69 >60
4/25/2022 12/25/2021 121 >120
4/25/2022 4/1/2022 ?? ??
4/25/2022 11/25/2021 ?? ??
4/25/2022 1/15/2022 ?? ??
Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

To compare dates they must first be in epoch (integer) format.  Use the strptime function to do that.  Then you can subtract to the difference.  The result will be in seconds so divide by 86400 to get days.  The Metric field is just a case statement.

| eval current_epoch=strptime('Current-Date', "%m/%d/%Y"), open_epoch=strptime('Open-Date', "%m/%d/%Y")
| eval diff=(current_epoch - open_epoch) / 86400
| eval Metric=case(diff<30,"<30", diff>120,">120", diff>60,">60", diff>30,">30")
| rename diff as "Diff Days"

BTW, try to avoid hyphens in field names as they just complicate the SPL.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

ajdyer2000
Path Finder

thank you 

0 Karma

PickleRick
SplunkTrust
SplunkTrust

Main question here is what is the form of your dates. If they are unix timestamps, just substract one from another and you're home. If they're strings (as in your example), you have to first parse them to timestamps.

<your search>
| eval opentimestamp=strptime('Open-Date',"%m/%d/%Y")
| eval currenttimestamp=strptimr('Current-Date',"%m/%d/%Y")
| eval diffdays=(currenttimestamp-opentimestamp)/86400

richgalloway
SplunkTrust
SplunkTrust

To compare dates they must first be in epoch (integer) format.  Use the strptime function to do that.  Then you can subtract to the difference.  The result will be in seconds so divide by 86400 to get days.  The Metric field is just a case statement.

| eval current_epoch=strptime('Current-Date', "%m/%d/%Y"), open_epoch=strptime('Open-Date', "%m/%d/%Y")
| eval diff=(current_epoch - open_epoch) / 86400
| eval Metric=case(diff<30,"<30", diff>120,">120", diff>60,">60", diff>30,">30")
| rename diff as "Diff Days"

BTW, try to avoid hyphens in field names as they just complicate the SPL.

---
If this reply helps you, Karma would be appreciated.
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!

SOK it to Me: Top 3 Benefits of Using Splunk Operator on Kubernetes that’ll Make ...

    Thursday, July 9, 2026  |  11:00AM–12:00PM PDT Duration: 1 hour (includes Q&A) Managing can feel like a ...

Upgrade Prep for 10.4, Network Observability Deep Dives, and More from Splunk Lantern

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

Splunk Developer Day announcements: AI agents, MCP tools, Forecasting, and Custom ...

Splunk Developer Day was packed with product and platform updates for developers building in the AI ...