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
Gnana Jegan SGnana Jegan S 

How to calculate Weekends and Different month total value?

I have a scenario to caluculate total leave month wise while employee apply leave.
Following scenario need to be cover:
  1. If you have taken leave on Friday and apply leave for the next five week days, the weekends will not be considered as leave and a total of 6 days leave w i l l be deducted. (considering saturday and sunday as weekoffs).
  2. If you have taken leave on Friday and apply leave for the next six week days ,then the weekends will also be considered as leave and a total of 11 days leave will be deducted. (2 saturdays and 2 sundays are considered as leave days ).
    Integer currentMonthCount = 0;
                    Integer prevLeave = 0;
                    Integer currentMonth = LeaveObj.From__c.month();
                    for(Integer i=0; i <= LeaveObj.From__c.daysBetween(LeaveObj.To__c); i++)  
                    {  
                        Date dt = LeaveObj.From__c + i;  
                        DateTime currDate = DateTime.newInstance(dt.year(), dt.month(), dt.day());  
                        String todayDay = currDate.format('EEEE');  
                        if(todayDay != 'Saturday' && todayDay !='Sunday' && (!holidaysSet.contains(dt)))
                        {  
                            if(currentMonth == dt.month()){
                                currentMonthCount++;                        
                            }else{ 
                                prevLeave = monthlyLeaveCount[dt.month()];
                                monthlyLeaveCount[dt.month() - 2] = +prevLeave + +currentMonthCount;
                                currentMonth = dt.month();
                                currentMonthCount = 1;
                            }  
                            workingDays = workingDays + 1;  
                        } else if(todayDay == 'Saturday' || todayDay =='Sunday'){
                            weekEnds = weekEnds + 1;
                        }
                    }            
                    prevLeave = monthlyLeaveCount[LeaveObj.To__c.month()];
                    monthlyLeaveCount[LeaveObj.To__c.month() - 1] = +prevLeave + +currentMonthCount;            
                }
            if(workingDays == 0){
                return LeaveObj;
            }
            if(workingDays > 6){
                if(weekEnds > 2){
                    workingDays = workingDays + weekEnds;
                }
            }