locked
how to find EndDate by using javascript in crm 2013? RRS feed

  • Question

  • Hi,

    I want to calculate EndDate according to given duration period except weekdays (saturday, sunday)

    scenario is like that

    StartDate=26-March-2014

    Duration=5 days

    EndDate should be 1-April-2014 (after removing saturday, sunday)

    Calculate EndDate after 5 working days

    Wednesday, March 26, 2014 8:47 AM

All replies

  • First you should use Add days method to get the end date. Say in you case end date is 31-March-2013. Now use the following function by passing start and end dates and calculate working days:-
    function GetBusinessDays(startDate, endDate)
    {
        if (startDate != null && endDate != null)
        {
            var cycletime = (Math.abs(endDate - startDate) / 86400000) + 1;
            var startday = startDate.getDay();
            var x = startday; // day of the week
            var y = 0; // number of business days for output
            var z = 0; // augment up to the total number of days
    
            while (z < cycletime) {
                if (x > 0 && x < 6) 
                {
                    y++;
                }
                x++;
                z++;
                if (x > 6)
                {
                    x = 0;
                }
            }
    
            return y;
        }
        return null;
    }


    Regards Faisal

    Wednesday, March 26, 2014 9:54 AM
  • Hi Faisal,

    I want to find endDate I just insert startDate and duration period. according to both fields enddate should be calculated except saturday sunday

    Wednesday, March 26, 2014 10:51 AM
  • Here is my script but its not working correctly I hope you understand my scenario after view my script. If you got my problem please tell me how to get EndDate

    function getStartDate()
    {
    var startDate=Xrm.Page.getAttribute("scheduledstart").getValue();
    var duration=Xrm.Page.getAttribute("new_estimateddurationindays").getValue();
    duration=Math.floor(duration);
    //alert(duration);
    //alert(startDate);
    //var startDate = Xrm.Page.getAttribute("scheduledstart").getValue();
    var endDate=startDate;
    //alert(endDate);
    var day=endDate.getDay();
    //day=day-1;
    //alert("dayname");
    //alert(day);

    for (var i=1;i<duration;i++)
    {
    if (day==6 || day==0)
    {
    alert("sorry");
    }
    else
    {
    duration=duration-1;
    endDate.setDate(endDate.getDate()+1);
    alert(endDate);
    }
    }

    }

    Wednesday, March 26, 2014 10:54 AM
  • Try this :-

    Number.prototype.mod = function(n) { return ((this%n)+n)%n; } Date.prototype.addBusDays = function(dd) { var wks = Math.floor(dd/5); var dys = dd.mod(5); var dy = this.getDay(); if (dy === 6 && dys > -1) { if (dys === 0) {dys-=2; dy+=2;} dys++; dy -= 6;} if (dy === 0 && dys < 1) { if (dys === 0) {dys+=2; dy-=2;} dys--; dy += 6;} if (dy + dys > 5) dys += 2; if (dy + dys < 1) dys -= 2; this.setDate(this.getDate()+wks*7+dys); } var due = Xrm.Page.getAttribute("scheduledstart").getValue(); due.addBusDays(5);

    alert(due);


    

    Regards Faisal

    Wednesday, March 26, 2014 11:38 AM