Getting Data In

Calculate Age from 2 Timestamp fields

ctripod
Explorer

Hi All!

I have a field in my data which represents DOB in a YYYYMMDD format. I'm trying to compare that DOB Timestamp with another Timestamp field which represents the time the event was created. Same format YYYYMMDD. What I'm trying to determine is how old someone is when they were tested.

I've attempted to convert them both to epoch using eval, but it appears to only work on the first field.

index=test | eval DOB = strftime(Date_of_Birth, "%Y%m%d") | eval ICD = strftime(Instance_Creation_Date, "%Y%m%d") | eval diff = ('DOB' - 'ICD')

Anyone have a pointer?

As always. Thanks!

0 Karma

elliotproebstel
Champion

For the direction you're looking to convert, I think you want strptime rather than strftime.

0 Karma

s2_splunk
Splunk Employee
Splunk Employee

The strftime function won't work for any data prior to the start of epoch in 1970. To do this properly, you will have to do your math discreetly. Check the accepted answer here for an approach.
You'll have to massage the RegEx a bit, because you don't have any separators between your date components. Something like this should work: rex field=Date_Of_Birth "(?<y>\d{4})(?<m>\d{2})(?<d>\d{2})"

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...