+ Start a Discussion
Shane QuiringShane Quiring 

Calculate the number of month left between two dates

Hi Experts,

I need some assitance with a date formula. I trying to calculate the number of months left between two dates, start and end date. I currently have the folloiwng formula:

(Custom_End_Date__c - today ())/30

Which will give me the number of days left and if I divide the number by 30 and I should get the numbner of months. Unfortunately I am short by one month. For example if I have a end date of 12/01/2015 minus today, 30 July, would give me 124 days. If I divide that by 30 I get 4 months, or more exactly 4.13 months. But from July to Dec is 5 months.

Any assistance would be greatly appreciated.

Thank-you
Best Answer chosen by Shane Quiring
CyberJusCyberJus
Dividing by 30 is going to give you slightly inaccurate results because not every month is 30 days. 

If you are only concerned about difference in the actual month - ie July 30th to Aug 1st = 1 month
Try this:
(MONTH(Custom_End_Date__c) + (12*(YEAR(Custom_End_Date__c)-YEAR(TODAY()))) - MONTH(TODAY())

All Answers

William TranWilliam Tran
use monthsBetween, and if you need to take the days into account then add 1  if needed:

Here's any example of the syntax:

Date a = Date.newInstance(2013,10,7);
Date b = Date.newInstance(2014,1,12);
Integer monthDiff = a.monthsBetween(b);
if (b.day() > a.day()) monthDiff++;

Thx
 
kaustav goswamikaustav goswami
What if you use the month function?

ABS(Month(End_Date__c) - Month(Start_Date__c))

Thanks,
Kaustav
CyberJusCyberJus
Dividing by 30 is going to give you slightly inaccurate results because not every month is 30 days. 

If you are only concerned about difference in the actual month - ie July 30th to Aug 1st = 1 month
Try this:
(MONTH(Custom_End_Date__c) + (12*(YEAR(Custom_End_Date__c)-YEAR(TODAY()))) - MONTH(TODAY())
This was selected as the best answer
Shane QuiringShane Quiring
Thanks Cyber Jus, 

That worked perfectly. 

I apreciate the help. 
Anna Proviz 16Anna Proviz 16
 Hi Shane,

When I try to use Cyber's function I get this error:
"Syntax error. Missing ')'"

Can you please help me to fix it? 

Thanks in advance
LinThawLinThaw
(MONTH(End_Date__c) + (12*(YEAR(End_Date__c)-YEAR(Start_Date__c))) - MONTH(Start_Date__c))