Splunk Enterprise

how to convert time format in search query

kirrusk
Communicator

could someone please help me to convert the time format.

time: Thu jul 20 18:49:57 2020  (string type)

i'm trying to get 2020-07-20 18:49:57

i want final result to  get diff between two dates , like  2020-07-20 18:49:57 -  2020-07-21 18:49:57 (in days)

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

To convert time strings into a different format, use a combination of strptime() and strftime().

 

... | eval newFormat=strftime(strptime(oldformatfield, "%a %b %d %H:%M:%S %Y"), "%Y-%m-%d %H:%M:%S")

 

To get the difference between two dates, however, you must use the parsed (epoch) form.

 

 

... | eval diff = strptime(oldformatfield1, "%a %b %d %H:%M:%S %Y") - strptime(oldformatfield2, "%a %b %d %H:%M:%S %Y")

 

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

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

To convert time strings into a different format, use a combination of strptime() and strftime().

 

... | eval newFormat=strftime(strptime(oldformatfield, "%a %b %d %H:%M:%S %Y"), "%Y-%m-%d %H:%M:%S")

 

To get the difference between two dates, however, you must use the parsed (epoch) form.

 

 

... | eval diff = strptime(oldformatfield1, "%a %b %d %H:%M:%S %Y") - strptime(oldformatfield2, "%a %b %d %H:%M:%S %Y")

 

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

kirrusk
Communicator

can u please suggest how to convert the diff values to days

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Let eval do the math for you.

| eval days = diff / 86400

 

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...