function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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))
Juliet HolidayJuliet Holiday
try this
(MONTH(Custom_End_Date__c) + (12*(YEAR(Custom_End_Date__c)-YEAR(TODAY()))) - MONTH(TODAY())
this is working for me 
i also use this quote for my blog https://budgetorbit.com/
wanis maywanis may

Hello Everyone, Thanks for sharing . We are currently working toată lumea va fi acum încântată să urmărească serialul turcesti online subtitrat în română.on seriale turcesti blog.  Bate la usa mea subtitrat in romana (https://clicksud.mobi/bate-la-usa-mea-subtitrat-in-romana/)
Jeniffer PogJeniffer Pog
You would need to tidy this up a little but theoretically all you need is:

var a = someDate;
var b = someOtherDate;

var c = Math.abs(a - b);
answer = c.getMonth();
Best Apk (https://homeofapk.com/
Jens Petter AbrahamsenJens Petter Abrahamsen
To get the number of months between two days: monthsBetween(secondDate) https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_date.htm#apex_System_Date_methods So something like: endDate.monthsBetween(startDate) (or the other way around) Should give you the number of months. Dividing by 30 would not be accurate, as not all months have 30 days.
wasim akram 75wasim akram 75
Hello guys,

I have been trying this but couldn't do it. Please help How to acheive it. 
 
  1. Field = Completed Release Cycle - Formula - Text - If Completed On month is between December and May then it should return like WI + Completed On year. Ex: WI 21. Below are other conditions that are applicable
    1. Between December and May then WI
    2. Between April and July then SP
    3. Between August and November then SU
Anky KolaAnky Kola
Great answer by peoples. Thanks.
best friendship quotes (https://lovequotes.co.in/friendship-quotes/)
TobyKutlerTobyKutler
Try this: 

IF(DAY(EndDate) < DAY(StartDate), (MONTH(EndDate)) + (12*(YEAR(EndDate)-(YEAR(StartDate)))) - (MONTH(StartDate)) - 1, (MONTH(EndDate)) + (12*(YEAR(EndDate)-YEAR(StartDate))) - MONTH(StartDate))
 
Divyesh DudhatDivyesh Dudhat
Hi Everyone,

Try this to get the full months.

Logic: M(Date) + (12 * (Y(Date) - Y(Today)) -  M(Today) + IF((EOMD(Today) - D(Today) + D(Date) - EOMD(Date) )/EOMD(Today)) > 0 , -1, 0)

SF Formula: 

FLOOR(MONTH( Date__c ) + (12*(YEAR( Date__c )-YEAR(TODAY()))) - MONTH(TODAY()))

IF(((DAY(DATE(YEAR(TODAY()),MONTH(TODAY())+1,1)-1) - DAY(TODAY())
+ DAY(Date__c ) - DAY(DATE(YEAR(Date__c ),MONTH(Date__c )+1,1)-1))/DAY(DATE(YEAR(TODAY()),MONTH(TODAY())+1,1)-1)) < 0, -1, 0)

Thanks
Abhishek KoriAbhishek Kori
Got the same issue now it is fixed.
Thanks to all for answering the question.
Read more about admin skillset. (https://www.cloudycoders.com/blog/salesforce-integration-by-cloudy-coders/)
Amal AdamAmal Adam
Hello, currently I am facing the same situation in calculating number of months in my Yacht rental business ( https://arinadubai.com/yacht-rental-dubai ). The above mentioned resolution is not working I think the code has been depricated. So, please if anyone know the right code, please let me know.