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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript - Calculate Number of Business Days between Two Dates

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

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

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

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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
    Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

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

    thanks.

  • Verified answer
    Community Member Profile Picture
    on at

    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
    on at

    Thanks everyone for your replies.

    Thanks

  • Community Member Profile Picture
    on at

    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


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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans