Getting Data In

How do I do mathematical operations (subtraction) with two timestamps in the same event?

hmdoan
Explorer

I've been trying to:
1) convert two date stamps into epoch (timestamp and lastmodified). The lastmodified stamp will be more recent.
2) calculate if there is greater than a 7 day difference between the two
3) then display these events.

My time stamps are all in the %d/%m/%Y %H:%M:%S format.

index=netcool_daily name="Auto Ticket" | eval starttime=strptime(timestamp,"%m/%d/%Y %H:%M:%S") | eval endtime=strptime(lastmodified,"%m/%d/%Y %H:%M:%S") | eval difference=(endtime - starttime)/86400 | search difference > 7

I get nothing returned.

Tags (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

You say your timestamps are in "d/m/Y" format, but your strptime commands are using "m/d/Y" format. Which is the correct format?

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

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

You say your timestamps are in "d/m/Y" format, but your strptime commands are using "m/d/Y" format. Which is the correct format?

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

hmdoan
Explorer

Thanks! That was it. I had the wrong date format. Once I fixed that, the search worked great. Typo on my part.

0 Karma

woodcock
Esteemed Legend

Everything looks OK so the problem has to be that either (or both) starttime or endtime is not created and that is surely because either timestamp or lastmodified does not exist. Try this search and whichever field does not exist, make it exists (probably just fix a typo):

index=netcool_daily name="Auto Ticket" | eval starttime=strptime(timestamp,"%m/%d/%Y %H:%M:%S") | eval endtime=strptime(lastmodified,"%m/%d/%Y %H:%M:%S") | table timestamp lastmodified starttime endtime

hmdoan
Explorer

My stupid error:

The format is in "%Y/%m/%d %H:%M:%S"

0 Karma

richgalloway
SplunkTrust
SplunkTrust

So when you change your strptime commands to strptime(timestamp, "%Y/%m/%d %H:%M:%S") and strptime(lastmodified, "%Y/%m/%d %H:%M:%S") does it work?

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

hmdoan
Explorer

Yes - once i changed my time formatting to be consistent, it worked. The dangers of cut-n-paste!!! 🙂

0 Karma

richgalloway
SplunkTrust
SplunkTrust

You say your timestamps are in "d/m/Y" format, but your strptime commands are using "m/d/Y" format. Which is the correct format?

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

richgalloway
SplunkTrust
SplunkTrust

So when you change your strptime commands to strptime(timestamp, "%Y/%m/%d %H:%M:%S") and strptime(lastmodified, "%Y/%m/%d %H:%M:%S") does it work?

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

richgalloway
SplunkTrust
SplunkTrust

So when you change your strptime commands to strptime(timestamp, "%Y/%m/%d %H:%M:%S") and strptime(lastmodified, "%Y/%m/%d %H:%M:%S") does it work?

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

hmdoan
Explorer

Sorry. The format is Year/Month/Day H:M:S

0 Karma
Get Updates on the Splunk Community!

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...

Purpose in Action: How Splunk Is Helping Power an Inclusive Future for All

At Cisco, purpose isn’t a tagline—it’s a commitment. Cisco’s FY25 Purpose Report outlines how the company is ...

[Upcoming Webinar] Demo Day: Transforming IT Operations with Splunk

Join us for a live Demo Day at the Cisco Store on January 21st 10:00am - 11:00am PST In the fast-paced world ...