05\23\19 through 09/22/19 = 4 month
09/23/19 through 09/30/19 = 8 days
so we'll use (8/(365/12)) to calculate the Partial month value in our prorate multiplier
excepted Output:- prorate multi = (4+(8/(365/12)))/12 = 0.3553
public class Task { public static Integer test(Date d1, Date d2) { //Integer Yeardiff = d2.Year() -d1.Year(); Integer monthDiff = d1.monthsBetween(d2); Integer Days=Date.daysInMonth(d2.Year(),d2.month()); Decimal RemaingDays = Days - d2.day(); Decimal calculate_Partial = (RemaingDays /(365/12)); Decimal Prorate_Multiplier = (monthDiff + calculate_Partial)/12; System.debug(calculate_Partial); System.debug('Rm Days'+RemaingDays); // System.debug(Days); System.debug('Diff month' +monthDiff); System.debug(Prorate_Multiplier); return monthDiff; }}@shubham patil
try this one:
public class Task {
public static Decimal calculateProrateMultiplier(Date d1, Date d2) {
Integer fullMonths = d1.monthsBetween(d2);
// Calculate days difference for the partial month calculation
// Assuming `d2` is the last day inclusive for calculation
Integer endDay = d2.day();
Integer startDayInEndMonth = d1.addMonths(fullMonths).day();
Integer daysInPartialMonth;
if (d2 > d1.addMonths(fullMonths)) {
// If d2 is beyond the full months calculated from d1, calculate the actual days difference
daysInPartialMonth = endDay - startDayInEndMonth + 1; // +1 to include both start and end day
} else {
// No partial month to consider
daysInPartialMonth = 0;
}
Decimal partialMonthValue = Decimal.valueOf(daysInPartialMonth) / (365.0 / 12);
Decimal prorateMultiplier = (fullMonths + partialMonthValue) / 12;
System.debug('Partial Month Value: ' + partialMonthValue);
System.debug('Full Months: ' + fullMonths);
System.debug('Prorate Multiplier: ' + prorateMultiplier);
return prorateMultiplier.setScale(4);
}
}
debug: