+ Start a Discussion
David TomDavid Tom 

Calculate the number of days between two dates while excluding weekends.

I need to count the working days or leave of any employee excluding the weekends,then i need to write code for same.
Subhranshu MohantySubhranshu Mohanty
Hi David 

You can use this below code where salesforce provide the pre built methods of date.

public Decimal findNoOfDays( Date startDate , Date endDate ) {

Decimal NoOfDays = 0;

Date tempStartDate = startDate;

for( integer i = 1; tempStartDate <= endDate; i ++ ) {

if(tempStartDate.daysBetween(tempStartDate.toStartofWeek()) == 0 || tempStartDate.daysBetween(tempStartDate.toStartofWeek()) == -6) {

tempStartDate = tempStartDate.adddays(1); continue; } NoOfDays = NoOfDays+1; tempStartDate = tempStartDate.adddays(1);

}

return NoOfDays ;

}



Thanks,

If it is solve your problem then Mark it as best answer!!
 
Bhanu MaheshBhanu Mahesh
Check this link as well it has both formula and Apex versions
https://success.salesforce.com/answers?id=90630000000gv5PAAQ