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
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
Thanks everyone for your replies.
Thanks
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); }
@Francesc, thanks for your code share. can you tell how we can minus weekends and only show business days?
thanks.
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
Hey Arvinder,
you can try below :
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.
you can use the momentjs library, for examples connected to Dynamics CRM check my blog post
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156