Splunk Search

How to find days between today's date and field value date?

rkassabov
Path Finder

I am trying to subtract a field value date (Step Due Date) from today's date (nowstring) to determine if the number of days between them is less than half the SLA value.

This is the relevant part of my query but it doesn't seem to be working:

 | eval nowstring=strftime(now(), "%Y-%m-%d")
 | eval "Coming Due"=case(("Step Due Date"-nowstring) < (SLA / 2), 1, "Step Date Completed" != "", 0)

Here is an example value from "Step Due Date": 2018-06-18 12:11:07.890000

0 Karma
1 Solution

MuS
Legend

Hi rkassabov,

your dates are in various formats, and because of the " the case() statement uses literally "Step Due Date" as string.
Try this one instead, where Step Due Date becomes an epoch timestamp which then can be subtracted with now():

| makeresults 
| eval SLA="2", "Step Due Date"="2018-06-18 12:11:07.890000", step_date_epoch=strptime('Step Due Date', "%F %H:%M:%S.%6N"), nowstring=now() 
| eval "Coming Due"=case(('Step Due Date' - nowstring) < (SLA / 2), 1, "Step Date Completed" != "", 0)

Adapt the SPL to your needs.

Hope this helps ...

cheers, MuS

View solution in original post

MuS
Legend

Hi rkassabov,

your dates are in various formats, and because of the " the case() statement uses literally "Step Due Date" as string.
Try this one instead, where Step Due Date becomes an epoch timestamp which then can be subtracted with now():

| makeresults 
| eval SLA="2", "Step Due Date"="2018-06-18 12:11:07.890000", step_date_epoch=strptime('Step Due Date', "%F %H:%M:%S.%6N"), nowstring=now() 
| eval "Coming Due"=case(('Step Due Date' - nowstring) < (SLA / 2), 1, "Step Date Completed" != "", 0)

Adapt the SPL to your needs.

Hope this helps ...

cheers, MuS

rkassabov
Path Finder

Perfect, thank you!

0 Karma
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...