Splunk Search

How to calculate total month difference between dates?

dhavamanis
Builder

Can you please tell us, how to calculate total month difference between dates?

Example:

startDate=1/1/2013 00:00:00
endDate=current date

Month difference count = 36

0 Karma
1 Solution

somesoni2
Revered Legend

Try something like this

| gentimes start=-1 | eval startDate=strptime("1/1/2013 00:00:00","%m/%d/%Y %H:%M:%S") | table startDate  | eval endDate=now() | eval sametimethisyear=strptime(strftime(startDate,"%m/%d/").strftime(endDate,"%Y"),"%m/%d/%Y") | eval diffMonth=if(endDate>sametimethisyear,12*(tonumber(strftime(sametimethisyear,"%Y"))-tonumber(strftime(startDate,"%Y"))) + (tonumber(strftime(endDate,"%m"))-tonumber(strftime(sametimethisyear,"%m"))),12*(tonumber(strftime(sametimethisyear,"%Y"))-tonumber(strftime(startDate,"%Y"))) - (tonumber(strftime(sametimethisyear,"%m"))-tonumber(strftime(endDate,"%m"))))

View solution in original post

somesoni2
Revered Legend

Try something like this

| gentimes start=-1 | eval startDate=strptime("1/1/2013 00:00:00","%m/%d/%Y %H:%M:%S") | table startDate  | eval endDate=now() | eval sametimethisyear=strptime(strftime(startDate,"%m/%d/").strftime(endDate,"%Y"),"%m/%d/%Y") | eval diffMonth=if(endDate>sametimethisyear,12*(tonumber(strftime(sametimethisyear,"%Y"))-tonumber(strftime(startDate,"%Y"))) + (tonumber(strftime(endDate,"%m"))-tonumber(strftime(sametimethisyear,"%m"))),12*(tonumber(strftime(sametimethisyear,"%Y"))-tonumber(strftime(startDate,"%Y"))) - (tonumber(strftime(sametimethisyear,"%m"))-tonumber(strftime(endDate,"%m"))))
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 ...