Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Shane 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
CyberJus
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())

William 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 goswami
What if you use the month function?

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

Thanks,
Kaustav
CyberJus
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 Quiring
Thanks Cyber Jus,

That worked perfectly.

I apreciate the help.
Anna Proviz 16
Hi Shane,

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

LinThaw
(MONTH(End_Date__c) + (12*(YEAR(End_Date__c)-YEAR(Start_Date__c))) - MONTH(Start_Date__c))
Juliet 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 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 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);
Best Apk (https://homeofapk.com/
Jens 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 75
Hello guys,

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 Kola
best friendship quotes (https://lovequotes.co.in/friendship-quotes/)
TobyKutler
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 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 Kori
Got the same issue now it is fixed.
Thanks to all for answering the question.