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!

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