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!

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

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 ...