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!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...