1

I'm trying to convert MonthsBetween two date into a Decimal value. Example : Start Date : 01-01-2021 End Date : 03-15-2021

The monthbetween Start Date and End Date are 3, but i'm trying to have 2.5. Do you know how to calculate this ?

Thanks in advance,

2 Answers 2

1

I found a solution :

Integer monthDifff = a.monthsBetween(b);
Integer daysEnd = Date.daysInMonth(quote.SBQQ__EndDate__c.year(),quote.SBQQ__EndDate__c.month());
Decimal day_s = quote.SBQQ__StartDate__c.day();
Decimal day_e = quote.SBQQ__EndDate__c.day();
Decimal factor = 0;
Decimal terms = 0;
if(day_s != day_e){
   factor = (day_e - day_s)/daysEnd;
}
terms = monthDifff+factor;

Thanks,

0

Use daysBetween() instead of monthsBetween(), and then divide by the average number of days in a month to get an approximate value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .