查找Microsoft Dynamics CRM 2011中两个日期之间的工作日数

时间:2012-05-04 17:56:10

标签: javascript date dynamics-crm dynamics-crm-2011

所有,我正在尝试使用javascript根据开始和结束字段中的日期将一个整数设置为一个字段。我甚至不确定使用CRM javascript查找星期几的正确语法。到目前为止,这是我的代码。

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

}

3 个答案:

答案 0 :(得分:3)

试试这个:

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()返回基于0的星期几表示。 http://www.w3schools.com/jsref/jsref_getday.asp

如果您想计算两个日期之间的天数,请尝试以下方法:

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

}

答案 1 :(得分:0)

我终于找到了解决方案:随意使用每个人。 :)

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

答案 2 :(得分:0)

这是我的代码:

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