Splunk Search

Detecting and changing date format.

moinyuso96
Path Finder

I have a field "Date" as below. However, there are some inconsistency in the date format.  How can I get the "30/1/2021" and change it to "1/30/2021" following the rest of the date format?

 

Date
4/16/2021
3/31/2021
2/28/2021
30/1/2021
2/13/2021
Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

First, have a serious chat with the developers about which date format to use and stick with it.

It's possible to check the date and guess if the format is correct or not, but it's not foolproof.  Hence, the chat.

See if this query helps.

| makeresults | eval _raw="Date
4/16/2021
3/31/2021
2/28/2021
30/1/2021
2/13/2021" | multikv forceheader=1
```Everything above just sets up test data.  Don't include in the real query```
```Extract the first number, which should be a month number```
| rex field=Date "(?<mon>\d+)"
```Get the current month number```
| eval currMon=strftime(now(), "%m")
```If the mon number is too big then assume it's a date; 
if it's bigger than the current month then assume it's a date;
otherwise, use Date```
| eval newDate=if(mon>12 OR mon>currMon, strftime(strptime(Date,"%d/%m/%Y"), "%m/%d/%Y"), Date)
| table Date newDate
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

First, have a serious chat with the developers about which date format to use and stick with it.

It's possible to check the date and guess if the format is correct or not, but it's not foolproof.  Hence, the chat.

See if this query helps.

| makeresults | eval _raw="Date
4/16/2021
3/31/2021
2/28/2021
30/1/2021
2/13/2021" | multikv forceheader=1
```Everything above just sets up test data.  Don't include in the real query```
```Extract the first number, which should be a month number```
| rex field=Date "(?<mon>\d+)"
```Get the current month number```
| eval currMon=strftime(now(), "%m")
```If the mon number is too big then assume it's a date; 
if it's bigger than the current month then assume it's a date;
otherwise, use Date```
| eval newDate=if(mon>12 OR mon>currMon, strftime(strptime(Date,"%d/%m/%Y"), "%m/%d/%Y"), Date)
| table Date newDate
---
If this reply helps you, Karma would be appreciated.
Get Updates on the Splunk Community!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...