0

I am trying to calculate some amount depending on the days. For first month it will have only 15 days and the subsequent month will have full days and last month will have 25 days(First and last month days may vary). Below is my output


15 / 1 / 2022 --- Need remaining 15 days
1 / 2 / 2022  --- 28 days
1 / 3 / 2022  --- 31 Days
25 / 4 / 2022 --- Need remaining 5 days

I tried DaysInMonth method of date function

Date.daysInMonth(m.Year(), m.Month());

Its giving correct output for 2 and 3rd month but not able to get the remaining days in first and last month

I used Date.DaysInbetween method but its giving total number of days but I need split of every month. Please help

2 Answers 2

1

Your tried approach for using Date.daysInMonth(m.Year(), m.Month()); is correct, and it can help in giving you the correct number of days for a month where you have complete overlap over that month.

when you have partial overlap, at the beginning of your date range or at the end. You can still get the Date.daysInMonth() method and get the entire months days. Then subtract the number of days that have passed. This will give you the remaining days for terminal months.

For the months in between your start and end-months. Calculation is straight forward, You need to iterate through all the months and store their days. No need to do any calculation.

I have taken 4 Jan 2020 to 14 Jan 2021 as example.

Date startDate = Date.newInstance(2020, 1, 4); 
Date endDate = Date.newInstance(2021, 1, 14);
Map<String, Integer> resultMap = new Map<String, Integer>();

if(startDate > endDate) {
  System.debug('start date > end date'); //incorrect call or you can even swap dates
}

//calculate the starting overlap
resultMap.put(startDate.month() + '-' + startDate.year() , 
            Date.daysInMonth(startDate.year(), startDate.month()) - startDate.day()
        );

//Now put the days of in between months,excluding start and end months
Date nextMonth = (startDate.addMonths(1)).toStartOfMonth();
system.debug('nextMonth : ' + nextMonth);
Date toLastMonth = (endDate.addMonths(-1)).toStartOfMonth();
system.debug('LastMonth : ' + toLastMonth);

while(nextMonth <= toLastMonth) {
    resultMap.put(nextMonth.month() + '-' + nextMonth.year(), Date.daysInMonth(nextMonth.year(), nextMonth.month()));
    nextMonth = nextMonth.addMonths(1); //increment by 1 month
}

//ending overlap
resultMap.put(endDate.month() + '-' + endDate.year() ,
         Date.daysInMonth(endDate.year(), endDate.month()) - endDate.day()
        );

for(String k : resultMap.keyset()) {
    System.debug(k + ' => ' + resultMap.get(k));
}


This will print the following map, with keys being month-year, and remaining days as value.

Result Debug Log

4

It looks like you actually don't need to calculate only the number of days in a week, but the number of configured days for some month. You can store the number of days in configuration Map and check if configuration is defined - get configured value, if no - get a number of days in the month.

public Integer getConfiguredDaysInDateMonth(Date dateToCheck){
    Map<Integer, Integer> configuredNumberOfDays = new Map<Integer, Integer>{
        1 => 15,
        4 => 5
    };
    if(configuredNumberOfDays.containsKey(dateToCheck.month())){
        return configuredNumberOfDays.get(dateToCheck.month());
    } else {
        return Date.daysInMonth(dateToCheck.year(), dateToCheck.month());
    }
}

It is recommended to store values for configuredNumberOfDays in custom settings, or custom metadata, or even in some config object records.

You must log in to answer this question.

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