1

All, I am trying to use javascript to setValue a integer into a field based on the dates in a start and end field. I'm not even sure what the correct syntax for finding the day of the week with CRM javascript. Here is my code so far.

function netbdays_change() {

    var startday = Xrm.Page.getAttribute("new_quotestart").getValue();
    var endday = Xrm.Page.getAttribute("new_quoteend").getValue();

    cycletime = endday - startday;

    Xrm.Page.getAttribute("new_setcycletime").setValue(cycletime);

}
MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
Andrew Woodard
  • 545
  • 1
  • 7
  • 21

3 Answers3

3

Try this:

function netbdays_change() {

    var startday = Xrm.Page.getAttribute("new_quotestart").getValue().getDay();
    var endday = Xrm.Page.getAttribute("new_quoteend").getValue().getDay();

    cycletime = endday - startday;

    Xrm.Page.getAttribute("new_setcycletime").setValue(cycletime);

}

getDay() returns a 0 based representation of the day of the week. http://www.w3schools.com/jsref/jsref_getday.asp

If you want to calculate the number of days between 2 dates, try this:

function netbdays_change() {

    var startday = Xrm.Page.getAttribute("new_quotestart").getValue();
    var endday = Xrm.Page.getAttribute("new_quoteend").getValue();

    cycletime = Math.abs(endday - startday)

    Xrm.Page.getAttribute("new_setcycletime").setValue(cycletime / 86400000);

}
Jason Lattimer
  • 2,858
  • 15
  • 14
  • That works well for telling me the day of the week. Now I just need to know how many days there are between the dates. Can you just add or subtract days days from one another in CRM JS? Thank you for the help – Andrew Woodard May 04 '12 at 19:06
  • I revised my answer to also reflect calculating the number of days between 2 dates. – Jason Lattimer May 04 '12 at 19:36
  • The calculation is still not working to tell me how many days are between the two dates. I am getting an output of 1. Any Ideas. I appreciate the help. – Andrew Woodard May 04 '12 at 20:22
  • 2
    What dates are you entering and what output do you expect? – Jason Lattimer May 04 '12 at 20:25
0

I finally figured out the solution: Feel Free to use everyone. :)

function netbdays_change() {
    var startdays = Xrm.Page.getAttribute("new_dateqaassigned").getValue();
    var enddays = Xrm.Page.getAttribute("new_quotecomplete").getValue();

    var cycletime = Math.abs(enddays - startdays) / 86400000; // This first part now works

    startday = Xrm.Page.getAttribute("new_dateqaassigned").getValue().getDay();
    endday = Xrm.Page.getAttribute("new_quotecomplete").getValue().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 (x <= 7 && z <= cycletime) {
        if (x > 0 && x < 6) {
            y++;
        }
        x++;
        z++;
    }
    x = 0;
    while (x <= 7 && z <= cycletime) {
        if (x > 0 && x < 6) {
        y++;
    }
        x++;
        z++;
        if (x == 6) {
            x = 0;
        }
    }
    Xrm.Page.getAttribute("new_quotetotalcycletime").setValue(y);
}
glosrob
  • 6,631
  • 4
  • 43
  • 73
Andrew Woodard
  • 545
  • 1
  • 7
  • 21
  • 2
    Worth noting that doesn't take into consideration bank holidays or other non working days, nor does it take into account those countries where weekends are working days. – glosrob May 05 '12 at 21:11
0

Here's my code:

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;
}