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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Hours calculate in mod...
Power Automate
Unanswered

Hours calculate in model driven app excluding holidays and weekends

(0) ShareShare
ReportReport
Posted on by 32

Hi,

 

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

 

Thanks,

Krishna

Categories:
I have the same question (0)
  • Verified answer
    v-bofeng-msft Profile Picture
    on at

    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

     

     

     

  • krishnna Profile Picture
    32 on at

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

     

  • v-bofeng-msft Profile Picture
    on at

    Hi @krishnna ,

     

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

     

    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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 523 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 406 Moderator

#3
abm abm Profile Picture

abm abm 245 Most Valuable Professional

Last 30 days Overall leaderboard