Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

JavaScript - Calculate Number of Business Days between Two Dates

Posted on by Microsoft Employee

CRM 2013, looking for a JavaScript to calculate number of business days between two dates assuming 5 business days in a week.

Thanks for the help.

Rehal

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    Here's the reverse - calculate end date from start and number of working days:

    function calcEndDate(startDate,workdays){
                var count = 0;
                var curDate = new Date(startDate.valueOf());
                curDate.setDate(curDate.getDate() - 1);
                while (count < workdays) {
                    curDate.setDate(curDate.getDate() + 1);
                    var dayOfWeek = curDate.getDay();
                    if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
                        count++;
                }
                return curDate;
               
            }//calcEndDate


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    Thanks everyone for your replies.

    Thanks

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    finally achieved this with the following ...

    function LM (startDate, endDate)
    { 
    var endDate = Xrm.Page.getAttribute("new_enddate").getValue(); 
    var startDate = Xrm.Page.getAttribute("new_startdate").getValue();
    
    var count = 0;
        var curDate = startDate;
        while (curDate <= endDate) {
            var dayOfWeek = curDate.getDay();
            if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
               count++;
            curDate.setDate(curDate.getDate() + 1);
    }
    Xrm.Page.getAttribute("new_leavedays").setValue(count);
    
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    @Francesc, thanks for your code share. can you tell how we can minus weekends and only show business days?

    thanks.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    Hi Arv_rehal,

    try the following, is a four line solution :-)

    function DateDiff() {
        var datefrom = Xrm.Page.getAttribute("new_datefrom").getValue();
        var dateto = Xrm.Page.getAttribute("new_dateto").getValue();
    
        var days = (dateto - datefrom) / (1000 * 60 * 60 * 24);
        Xrm.Page.getAttribute("new_daydiff").setValue(days);
    }

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    Hey Arvinder,

    you can try below :

    stackoverflow.com/.../find-the-number-of-business-days-between-two-dates-in-microsoft-dynamics-crm-201

    however it wont be able applicable for bank holidays or other holidays in a week , i hope you are not expecting that too.

    mark my suggestion as verified if helpful.

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: JavaScript - Calculate Number of Business Days between Two Dates

    you can use the momentjs library, for examples connected to Dynamics CRM check my blog post

    www.crmanswers.net/.../momentjs-and-dynamics-crm.html

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans