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
Lavanya Ponniah 3

# how to calculate the leave taken which exclude weekends and public holidays

I have a Custom object Leave Application ,in that start date and end date are there and also the field leave_taken__c is there how to calculate the no.of leave taken between the two dates and which exclude the weekends and public holidays.
Alex Tennant
Assuming your working week is Monday to Friday you should be able to calculate the number of working days between two dates using the following Apex.

You could enhance this to determine your working days using the BusinessHours object if you wanted to.
```public Integer calculateWorkingDaysBetweenTwoDates(Date date1, Date date2)
{
List<Holiday> holidays = [SELECT StartTimeInMinutes, Name, ActivityDate FROM Holiday];

Integer allDaysBetween = date1.daysBetween(date2);
Integer allWorkingDays = 0;

for(Integer k = 0; k < allDaysBetween ;k++ )
{
{
allWorkingDays++;
}
}

return allWorkingDays;
}

public boolean checkifWorkingDay(Date currentDate, List<Holiday> holidays)
{
Date weekStart  = currentDate.toStartofWeek();

for(Holiday hDay : holidays)
{
if(currentDate.daysBetween(hDay.ActivityDate) == 0)
{
return false;
}
}

if(weekStart.daysBetween(currentDate) == 0 || weekStart.daysBetween(currentDate) == 6)
{
return false;
}
else
{
return true;
}
}```