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
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

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