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!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...