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!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...