Re: Hours calculate in model driven app excluding holidays and weekends
Hi @krishnna ,
I've made a test for your reference:

Result

Steps:
1\Edit command bar

2\Add a new command

3\input js file and set function

js code
function bofC (){
function test1(context) {
window.top.context = context;
window.top.formContext=context.getFormContext();
}
function carryTime(date) {
if (date.getMinutes() > 0 && date.getMinutes() < 15) {
date.setMinutes(0);
}
if (date.getMinutes() >= 15 && date.getMinutes() < 30) {
date.setMinutes(30);
}
if (date.getMinutes() > 30 && date.getMinutes() < 45) {
date.setMinutes(30);
}
if (date.getMinutes() >= 45) {
date.setHours(date.getHours() + 1);
date.setMinutes(0);
}
return date;
}
const holidyList="02-25;03-01;07-18" //set your holidy list
let workStartHour = 9 //Work Start Time
let workEndHour=17 //Work End Time
let getStartTime=Xrm.Page.getAttribute("crba2_starttime").getValue()
let getEndTime=Xrm.Page.getAttribute("crba2_endtime").getValue()
let halfHoursDiff=0;
let tampValue=0;
var timeTamp=new Date();
let timeTampStr="";
let number = DateDiffNoWeekDay(new Date(getStartTime), new Date(getEndTime));
function DateDiffNoWeekDay(startTime, endTime) {
if (startTime >= endTime) return 0;
startTime = carryTime(startTime);
endTime = carryTime(endTime);
halfHoursDiff=(endTime.getTime()-startTime.getTime())/1000/60/60*2;
tampValue=0
timeTamp=new Date();
for(let I=0;I < halfHoursDiff;I++){
timeTamp.setTime(startTime.getTime()+I*30*60*1000);
month=timeTamp.getMonth()+1
strDay=timeTamp.getDate()
if(month<10){month="0"+month};
if(strDay<10){strDay="0"+strDay};
timeTampStr=`${month}-${strDay}`
if(timeTamp.getDay()!==0 && timeTamp.getDay()!==0 && holidyList.indexOf(timeTampStr) === -1 && timeTamp.getHours() >= workStartHour && timeTamp.getHours() <workEndHour )
{
tampValue=tampValue+0.5
};
}
return tampValue
}
Xrm.Page.getAttribute("crba2_workinghours").setValue(number)
}
Please replace "crba2_starttime","crba2_endtime","crba2_workinghours" with your file's logic name
Best Regards,
Bof