Splunk Search

Strftime/Strptime not including leading zero

mistydennis
Communicator

DateField before eval: 20190402000000

I'm trying to apply strftime/strptime so the DateField will show as 2019-04-02

My eval:

| eval DateField=strftime(strptime('DateField',"%Y%m%d"), "%F") 

This eval produces 2019-04-20 instead of 2019-04-02. I thought %d included a leading zero, but it's not showing in my date. Where did I go wrong?

0 Karma
1 Solution

vnravikumar
Champion

Hi

Try

| eval DateField=strftime(strptime('DateField',"%Y%m%d%H%M%S"), "%Y-%m-%d")

View solution in original post

somesoni2
Revered Legend

I believe additional zeros in your timestamp are causing issue. Ideally, your strptime time format should include all characters appearing in your DateField, that way every character is properly processes. Give this a try (runanywhere sample, look for time format in DateField2)

| gentimes start=-1 | eval DateField="20190402000000" | table DateField  | eval DateField1=strftime(strptime('DateField',"%Y%m%d"), "%F")| eval DateField2=strftime(strptime('DateField',"%Y%m%d000000"), "%F")
0 Karma

mistydennis
Communicator

This also worked. Thanks so much, @somesoni2

0 Karma

vnravikumar
Champion

Hi

Try

| eval DateField=strftime(strptime('DateField',"%Y%m%d%H%M%S"), "%Y-%m-%d")

mistydennis
Communicator

That did it, thank you!

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...