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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...