You need to sign in to do that
Don't have an account?
pankaj kabra
Difference Between Two Dates Excluding Weekends
//Class for Calculating Working Dates between Two Days
public with sharing class TotalWorkingDyasBetweenTwoDates {
public static Double totalWorkingDays(Date startDate,Date endDate){
Double totalWorkingDays = 0;
Double temps;
Date tempDate = date.newInstance(1985, 6, 24);
//Calculating mod of no of days between random date and start date
Double temp = Math.mod(tempDate.daysBetween(startDate) , 7);
if(temp==0){
System.debug('hello 0');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 2;
}
else if(temps == 2 ){
totalWorkingDays = 3;
}
else if(temps == 3 ){
totalWorkingDays = 4;
}
else if(temps == 4 ){
totalWorkingDays = 5;
}
else if(temps == 5 ){
totalWorkingDays = 5;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 1;
}
else if(temp==1){
System.debug('hello 1');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 2;
}
else if(temps == 2 ){
totalWorkingDays = 3;
}
else if(temps == 3 ){
totalWorkingDays = 4;
}
else if(temps == 4 ){
totalWorkingDays = 5;
}
else if(temps == 5 ){
totalWorkingDays = 4;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 1;
}
else if(temp==2){
System.debug('hello 2');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 2;
}
else if(temps == 2 ){
totalWorkingDays = 3;
}
else if(temps == 3 ){
totalWorkingDays = 3;
}
else if(temps == 4 ){
totalWorkingDays = 3;
}
else if(temps == 5 ){
totalWorkingDays = 4;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 1;
}
else if(temp==3){
System.debug('hello 3');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 2;
}
else if(temps == 2 ){
totalWorkingDays = 2;
}
else if(temps == 3 ){
totalWorkingDays = 2;
}
else if(temps == 4 ){
totalWorkingDays = 3;
}
else if(temps == 5 ){
totalWorkingDays = 4;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 1;
}
else if(temp==4){
System.debug('hello 4');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 1;
}
else if(temps == 2 ){
totalWorkingDays = 1;
}
else if(temps == 3 ){
totalWorkingDays = 2;
}
else if(temps == 4 ){
totalWorkingDays = 3;
}
else if(temps == 5 ){
totalWorkingDays = 4;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 1;
}
else if(temp==5){
System.debug('hello 5');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 0;
}
else if(temps == 2 ){
totalWorkingDays = 1;
}
else if(temps == 3 ){
totalWorkingDays = 2;
}
else if(temps == 4 ){
totalWorkingDays = 3;
}
else if(temps == 5 ){
totalWorkingDays = 4;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 0;
}
else if(temp==6){
System.debug('hello 6');
temps = Math.mod(startDate.daysBetween(endDate) , 7);
if(temps == 1 ){
totalWorkingDays = 1;
}
else if(temps == 2 ){
totalWorkingDays = 2;
}
else if(temps == 3 ){
totalWorkingDays = 3;
}
else if(temps == 4 ){
totalWorkingDays = 4;
}
else if(temps == 5 ){
totalWorkingDays = 5;
}
else if(temps == 6 ){
totalWorkingDays = 5;
}
else
totalWorkingDays = 0;
}
else{
System.debug('sffsf');
totalWorkingDays = 999;
}
totalWorkingDays = totalWorkingDays + math.floor(startDate.daysBetween(endDate)/7*5);
return totalWorkingDays;
}
}
Ok..good one
I am not going inside code.Just a simple fundamental .
Do you know how to configure holidays in salesforce calender ?
With this you can define custom holidays .
Try this :Your Name>Setup>Administration Setup>Company Profile>Holidays
Also try Business Hours for the same .
Okay .Tell me one more thing is it necessary for you to use APEX code ?What if I suggest you WF rule ?
Ok .Thats very brave confession .
Let me tell you try your best to use WF rules(i.e WorkFlow rules) to achieve what you want.If you see thats not possible anyhow using WF rules then only go for APEX class or triggers .Always try to simplify things.
Now tell me is it ok if I work for you using WF rules ?
Agreed...
Sourav :-)