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)

working js function code to calculate number of leave days excluding weekends in ms dynamic

(0) ShareShare
ReportReport
Posted on by 1,345

below js function to calculate number of leave days excluding weekends(saturday & sunday) and set this on change of end date and it works.

function calculatedays()
{
var startDate;
var endDate;
var d = Xrm.Page.data.entity.attributes.get("new_start_date").getValue();
if(d!=null)
var d1= d.getDate();
var d2= d.getMonth();
d2++;
var d3= d.getFullYear();
startDate =d1+"/"+d2+"/"+d3;
var firstDate = new Date(d3,d2,d1);
var fms=firstDate.getTime();
}
else{
alert("Give Start Date");
return false;
}
var end = Xrm.Page.data.entity.attributes.get("new_end_date").getValue();
if (end != null) {
var e1= end.getDate();
var e2= end.getMonth();
e2++;
var e3= end.getFullYear();
endDate=e1+ "/" + e2+ "/" + e3;
var secondDate = new Date(e3,e2,e1);
var ems=secondDate.getTime();
}
var range_date_off=[startDate,endDate];
var d1 = parseDate(range_date_off[0])
var d2= parseDate(range_date_off[1])

var days= [];
for (var dt = d1; dt <= d2; dt.setDate(dt.getDate() + 1)) {
var fdt = new Date(dt)
var a=new Date(fdt.getFullYear(),(fdt.getMonth()),fdt.getDate());
if(a.getDay() != 6 && a.getDay() != 0)
{
days.push ( fdt.getDate() + "/" + (fdt.getMonth() + 1) + "/" + fdt.getFullYear());
}
}
var leavedays=days.length;
alert("you have leave"+leavedays);
Xrm.Page.data.entity.attributes.get("new_number_of_day").setValue(leavedays.toString());

// Xrm.Page.getAttribute("new_number_of_day").setSubmitMode("always");
}
function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[2], parts[1]-1, parts[0]);
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    DH kumar CRM Profile Picture
    1,345 on at

    thought to share this above working code if someone in the need because it will be useful for someone who ever need it as this can used AS-IS without any modification

  • DH kumar CRM Profile Picture
    1,345 on at

    Hi guido,

    Thanks for correction and but wanted to let you know that the code which i posted is working fine in my case and i will check with your modified code and post again.

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