Notifications
Announcements
No record found.
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
you can use the momentjs library, for examples connected to Dynamics CRM check my blog post
www.crmanswers.net/.../momentjs-and-dynamics-crm.html
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.
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
@Francesc, thanks for your code share. can you tell how we can minus weekends and only show business days?
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); }
Thanks everyone for your replies.
Thanks
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2