Splunk Search

How to convert a timestamp from one format to a different one?

xp001975
Explorer

convert 2023-03-15T17:25:18.832-0400 to YYYY-MM-DD HH:MM:SS.Millisec .

2023-03-15T17:25:18.832-0400 ------------------- > 2023-03-15 17:25:18.832

Once converted to the asked format i need to calculate the difference between EndTime & StartTIme.

Labels (1)
0 Karma

bowesmana
SplunkTrust
SplunkTrust

User strptime and strftime to Parse and Format time. Run this example in a search window

| makeresults
| fields - _time
| eval t="2023-03-15T17:25:18.832-0400"
| eval epoch_t=strptime(t, "%FT%T.%Q%z")
| eval formatted_t=strftime(epoch_t, "%F %T.%Q")

See here for time format variables

https://docs.splunk.com/Documentation/Splunk/9.0.3/SearchReference/Commontimeformatvariables

In order to calculate differences in time, always convert them to epoch times then simply subtract and you will get difference in seconds

You can run this example in a search window

| makeresults
| fields - _time
| eval StartTime="2023-03-15T17:25:18.832-0400"
| eval EndTime  ="2023-03-15T19:37:18.123-0400"
| eval epoch_start_time=strptime(StartTime, "%FT%T.%Q%z")
| eval epoch_end_time  =strptime(EndTime, "%FT%T.%Q%z")
| eval difference = epoch_end_time - epoch_start_time

 

0 Karma

xp001975
Explorer

xp001975_0-1678931288486.png

No the difference is coming as expected 

0 Karma

bowesmana
SplunkTrust
SplunkTrust

I am not sure what you mean by 'coming as expected'. 

This is an example that shows you how to parse time and calculate differences and uses simple fixed examples.

You need to map this onto your use case with your data.

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

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...