Splunk Search

How to convert 12 hours to 24 hours?

angelinealex
Communicator

I want to convert my date field from 12 hours to 24 hours.

I have the date field as "2/27/2018 10:21:03 PM" and would like to convert as "2018-03-27 22:21:03"

I tried the below and it didn't work.

| eval epochtime=strptime("2/27/2018 10:21:00 PM", "%m/%d/%Y %H:%M:%S %p")
| eval DSyncTime=strftime(epochtime, "%Y-%m-%d %H:%M:%S")

Output i am getting as 2018-02-27 10:41:00.

Its not considering AM/PM.

Can someone help me please.

Tags (3)
0 Karma
1 Solution

niketn
Legend

@angelinealex, you would need to convert your timestamp in data using %I i.e. 12 hour clock in the strptime() function and then convert the same back to strftime() using %H for 24 hour clock. PS: I have used %p in strftime() for validating the AM/PM is being picked up as expected.

| makeresults
| eval epochtime=strptime("2/27/2018 10:21:03 PM","%m/%d/%Y %I:%M:%S %p")
| eval DSyncTime=strftime(epochtime, "%Y-%m-%d %H:%M:%S %p")

Please refer to Splunk Documentation for strptime()/strftime() time formats

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

niketn
Legend

@angelinealex, you would need to convert your timestamp in data using %I i.e. 12 hour clock in the strptime() function and then convert the same back to strftime() using %H for 24 hour clock. PS: I have used %p in strftime() for validating the AM/PM is being picked up as expected.

| makeresults
| eval epochtime=strptime("2/27/2018 10:21:03 PM","%m/%d/%Y %I:%M:%S %p")
| eval DSyncTime=strftime(epochtime, "%Y-%m-%d %H:%M:%S %p")

Please refer to Splunk Documentation for strptime()/strftime() time formats

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

angelinealex
Communicator

It worked. Thank you very much.

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!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...