web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id : L0fxdvx6fnd8Jx+i4HC9gK
Power Automate - Building Flows
Answered

Hours calculate in model driven app excluding holidays and weekends

Like (0) ShareShare
ReportReport
Posted on 10 Apr 2023 17:55:17 by 32

Hi,

 

I need to calculate business hours taken between two data fields in model driven app excluding holidays and weekends.

 

Thanks,

Krishna

I have the same question (0)
  • v-bofeng-msft Profile Picture
    on 12 Apr 2023 at 01:13:31
    Re: Hours calculate in model driven app excluding holidays and weekends

    Hi @krishnna ,

     

    It should be possible, and I suggest you find a developer of your org's CRM for help.

     

    Best Regards,
    Bof

  • krishnna Profile Picture
    32 on 11 Apr 2023 at 18:42:31
    Re: Hours calculate in model driven app excluding holidays and weekends

    It is nice .Can we read holidays from the crm system through javascript.

     

  • Verified answer
    v-bofeng-msft Profile Picture
    on 11 Apr 2023 at 06:05:32
    Re: Hours calculate in model driven app excluding holidays and weekends

    Hi @krishnna ,

     

    I've made a test for your reference:

    vbofengmsft_0-1681192844444.png

    Result

    vbofengmsft_1-1681192861445.png

    Steps:

    1\Edit command bar

    vbofengmsft_2-1681192908560.png

    2\Add a new command

    vbofengmsft_3-1681192965287.png

    3\input js file and set function

    vbofengmsft_4-1681192998442.png

    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

     

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

MS.Ragavendar – Community Spotlight

We are honored to recognize Ragavendar Swaminatha Subramanian as our September…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 783 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 515 Moderator

#3
trice602 Profile Picture

trice602 299 Super User 2025 Season 2

Last 30 days Overall leaderboard
Loading started
Loading complete