Splunk Search

Why am I unable to find the time difference between two dates with my current eval statements?

changux
Builder

Hi all.

I have one field called date1 with a timestamp like this:

5/7/16 16:35

I need the time difference (just for the date) in days against now(). I am using this expression:

...  | eval onlydate=strftime(strptime(date1,"%-m/%d/%y %H:%M"),"%-m/%-d/%y") | eval nowstring=strftime(now(), "%-m/%-d/%y")

And works well, but I can't calculate the time difference between nowstring and onlydate and both are in the same format. Any clue? I tried using

... | eval difference=(nowstring - onlydate)

And didn't work.

Thanks!

0 Karma
1 Solution

sundareshr
Legend

strftime is a string format. You need to change to epoch time or a number to do math. So try this

| eval onlydate=strptime(date1,"%-m/%d/%y %H:%M") | eval datediff=tostring(now() - onlydate, "duration")

View solution in original post

sundareshr
Legend

strftime is a string format. You need to change to epoch time or a number to do math. So try this

| eval onlydate=strptime(date1,"%-m/%d/%y %H:%M") | eval datediff=tostring(now() - onlydate, "duration")

changux
Builder

Great, thanks. Datediff result is in epoch, how i can convert to human? i tried with:

. | eval formatted_time=strftime(datediff/1000, "%H:%M:%S %d-%m-%Y")

Without luck.

0 Karma

sheamus69
Communicator

Something like:

...|eval formatted_time=strftime(datediff,"%F %T")

Should do the trick.

0 Karma

sundareshr
Legend

datediff is in seconds. duration format is days+hours:mins:seconds.microseconds. what format are you looking for?

0 Karma

sundareshr
Legend

You could do something like this

...| eval onlydate=strptime(date1,"%-m/%d/%y %H:%M") | eval datediff=tostring(round(now() - onlydate, 0), "duration") | eval datediff= replace(datediff,"(\d*)\+?(\d+)\:(\d+)\:(\d+)","\1d \2h \3min \4s")

changux
Builder

Thanks a lot!

0 Karma

changux
Builder

My datediff field has values like 6+08:19:34.000000.

0 Karma
Get Updates on the Splunk Community!

Splunk Observability Synthetic Monitoring - Resolved Incident on Detector Alerts

We’ve discovered a bug that affected the auto-clear of Synthetic Detectors in the Splunk Synthetic Monitoring ...

Video | Tom’s Smartness Journey Continues

Remember Splunk Community member Tom Kopchak? If you caught the first episode of our Smartness interview ...

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud?

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud? Learn how unique features like ...