What Is The Method To Calculate Months Between Dates?

298    Asked by ChloeBurgess in Salesforce , Asked on Sep 22, 2023

What is the method of calculating the duration between two given dates by utilizing the formula field? Whenever the user denotes the Start Date and End Date, there is a requirement to calculate the Subscription Term. This must be actual months. I need suggestions on what correction must be made in the formula so that The exact months can be calculated between two given dates (including the case of leap year in the middle). For example, if the start date is 1st Jan 2000 and the end date is 15th March 2000, then it must display something as in 2.5 months rather than 3. We are using the formula at present.

IF( IF( SBQQ__SubscriptionTerm__c > 0 , SBQQ__SubscriptionTerm__c , (( SBQQ__EndDate__c - SBQQ__StartDate__c) / 30.4375) ) = 0,

An exact term is needed since the amount that is applicable to products is dependent on the term and it is not possible to round off that term. It can contribute to improper amount calculator. I need a solution and it will be great if anyone can offer a solution utilizing APex as well.


Answered by Dhruv tiwari

You can deploy YEAR and MONTH to calculate the months between two given dates. You can try the code as well.

  (YEAR(SBQQ__EndDate__c) * 12 + (MONTH(SBQQ__EndDate__c) - 1) - YEAR(SBQQ__StartDate__c) * 12 + (MONTH(SBQQ__StartDate__c) - 1) )

The above-mentioned code should provide you with the appropriate result in every case. However, partial months will also be counted; for example, 31st January to 1st February will be taken as an entire month.


Your Answer

Interviews

Parent Categories