Tuesday, July 5, 2011

Calc the working days between two days including the public holidays

Using below code we can find the number of working days between two dates inculding the public holidays . I wrote this code long back for one POC .I guess it will work :-) .

Req : I have to find working days between two dates ie : i have to remove weekends/ Public Holidays between two dates



function Service_PreInvokeMethod (MethodName, Inputs, Outputs)
{
if (MethodName=="Working Days")
{
//MM DD YYYY format
var StartDate = Inputs.GetProperty("StartDate");
var EndDate = Inputs.GetProperty("EndDate");
var start= new Date(StartDate);
var end = new Date(EndDate);
var abc=0;
// On application fill the Public Holidays from Application
// Under Adm-Service update all the public holidays

var sBO=TheApplication().GetBusObject("Shift");
var sBC=sBO.GetBusComp("Shift Exception Hour");

with(sBC)
{
SetViewMode(AllView);
ActivateField("Exception Id");
ActivateField("Start Date");
ClearToQuery();
SetSortSpec("Start Date(DESCENDING)");
SetSearchSpec("Exception Id","1-1JJB");
// Row id is the Schdule name
//Better give the Name instead of rowid .
ExecuteQuery(ForwardBackward);
var irec=FirstRecord();


while(irec)
{
var Holiday =GetFieldValue("Start Date");
var Holiday1=new Date(Holiday);

if(Holiday1.getDay()!=0 && Holiday1.getDay()!=6 )
{
if(Holiday1.getTime() >= start.getTime() && Holiday1.getTime() <= end.getTime())
{
abc=abc+1;
}

}
if (Holiday1.getTime() < start.getTime())
{
break;
}
else
{
irec=NextRecord();
}

}


}

sBC=null;
sBO=null;


var diffDays= Math.floor((end-start)/1000/60/60/24);
var weeksBetween=Math.floor(diffDays/7);
var startDay=start.getDay();
var endDay =end.getDay();
if(start.getDay()==end.getDay())
{
var adjust=0;

}


else
{
if (start.getDay()==0 && end.getDay()==6)
{
var adjust=5;

}

else
{
if(start.getDay()==6 && end.getDay()==0)
{
var adjust=0;
}
else
{
if(end.getDay()==6 || end.getDay==0)
{
var adjust=5-start.getDay();
}
else
{
if (start.getDay()==0 || start.getDay==6)
{
var adjust=end.getDay();
}
else
{
if(end.getDay()>start.getDay())
{
var adjust=end.getDay()-start.getDay();
}
else
{
var adjust =5+end.getDay()-start.getDay();
}
}


}

}

}


}


var workingDaysCalc=(weeksBetween*5)+adjust-abc;



Outputs.SetProperty("workingDaysCalc",workingDaysCalc);

// TheApplication().RaiseErrorText("Start Date :"+start +"End Date :"+end +"ABC:"+abc+"Working "+workingDaysCalc);
return(CancelOperation);
}

return (ContinueOperation);
}


1 comment:

  1. Hi,
    you can acomplish this by using:
    BS: "FS Holiday API Service"
    Method: GetElapsedBusinessTime

    ReplyDelete