Splunk Search

How to convert a String date format to another date format?

ewise1
New Member

Hi,

I have a date that comes in as part of a string, and it looks like "Jun 28 11:50:23 2017". How can I convert this to show "Jun 28 2017 11:50:23"?

Tags (1)
0 Karma
1 Solution

niketn
Legend

Try the following run anywhere search

| makeresults 
| eval Time="Jun 28 11:50:23 2017"
| eval Time=strptime(Time, "%b %d %H:%M:%S %Y")
| fieldformat Time=strftime(Time,"%b %d %Y %H:%M:%S")

PS: final fieldformat command is just displaying epoch time field Time to human readable string time as per your need. You might have to use eval instead of fieldformat is you want to use String Time down the line in your Splunk Search rather than epoch time.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

0 Karma

niketn
Legend

@cpetterborg, thanks for reminding of sed, following replace should also do the trick

 | makeresults 
 | eval Time="Jun 28 11:50:23 2017"
 | eval Time=replace(Time,"(\w{3}\s\d{2})(\s\d{2}:\d{2}:\d{2})(\s\d{4})","\1\3\2")

Basically June 28 is extracted as 1st Capturing group 11:50:23 as 2nd and 2017 as 3rd. Then replace is used to format them in correct sequence i.e. 1, 3, 2. Output field will remain String Date. Refer to the documentation: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/TextFunctions#Basic_example_4

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

cpetterborg
SplunkTrust
SplunkTrust

This will also work in your case, without using as much processing:

 | makeresults 
 | eval Time="Jun 28 11:50:23 2017"
 | rex field=Time mode=sed "s/(\w+\s\d+)\s(\d+:\d+:\d+)\s(\d+)/\\1 \\3 \\2/"

niketn
Legend

Try the following run anywhere search

| makeresults 
| eval Time="Jun 28 11:50:23 2017"
| eval Time=strptime(Time, "%b %d %H:%M:%S %Y")
| fieldformat Time=strftime(Time,"%b %d %Y %H:%M:%S")

PS: final fieldformat command is just displaying epoch time field Time to human readable string time as per your need. You might have to use eval instead of fieldformat is you want to use String Time down the line in your Splunk Search rather than epoch time.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
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!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...