Javascript 如何检查两个日期之间的天数是否为工作日?

Javascript 如何检查两个日期之间的天数是否为工作日?,javascript,datetime,Javascript,Datetime,我有一个表单,用户从日历中获取两个日期。我想检查用户选择的两个日期之间的天数是否从星期五到星期一(包括) 我找到了计算工作日(包括星期六和星期日)的脚本: 函数calcBusinessDays(dDate1、dDate2){ var iWeeks、iDateDiff、Idejust=0; 如果(dDate25)和&(iWeekday2>5))iAdjust=1;//如果两天都在周末,则进行调整 iWeekday1=(iWeekday1>5)?5:iWeekday1;//仅统计工作日 iWeekd

我有一个表单,用户从日历中获取两个日期。我想检查用户选择的两个日期之间的天数是否从星期五到星期一(包括)

我找到了计算工作日(包括星期六和星期日)的脚本:

函数calcBusinessDays(dDate1、dDate2){
var iWeeks、iDateDiff、Idejust=0;
如果(dDate25)和&(iWeekday2>5))iAdjust=1;//如果两天都在周末,则进行调整
iWeekday1=(iWeekday1>5)?5:iWeekday1;//仅统计工作日
iWeekday2=(iWeekday2>5)?5:iWeekday2;
//以周为单位计算差异(1000毫秒*60秒*60分钟*24小时*7天=60480万)
iWeeks=Math.floor((dDate2.getTime()-dDate1.getTime())/604800000)

如果(iWeekday1很快就把它组合起来,但它应该会起作用。 我很难理解你的示例代码。我只是觉得这可能会更好一些

var calcBusinessDays = function (dDate1, dDate2) {
    //We are working with time stamps
    var from = dDate1.getTime()
    ,   to = dDate2.getTime()
    ,   tempDate = new Date()
    ,   count = 0;

    //loop through each day between the dates 86400000 = 1 day
    for(var _from = from; _from < to; _from += 86400000){
        //set the day
        tempDate.setTime(_from);
        //If it is a weekend add 1 to count
        if ((tempDate.getDay() <= 1) || (tempDate.getDay() >= 5)) {
            count++;
        }
    }

    //return count =)
    return count;
}
var calcBusinessDays=函数(dDate1、dDate2){
//我们正在使用时间戳
var from=dDate1.getTime()
,to=dDate2.getTime()
,tempDate=新日期()
,计数=0;
//在日期86400000=1天之间循环每天
对于(var _from=from;_from
这将为周五、周六、周日和周一添加1。
如果您想要其他日期,唯一需要更改的行是嵌套在for循环中的if语句。

您是在问如何获取两个日期之间的天数而不是工作日?我知道这可能是语言障碍问题,但您能否更具体一点。我真的不明白您到底想要“预期”的天数结果是什么?
var calcBusinessDays = function (dDate1, dDate2) {
    //We are working with time stamps
    var from = dDate1.getTime()
    ,   to = dDate2.getTime()
    ,   tempDate = new Date()
    ,   count = 0;

    //loop through each day between the dates 86400000 = 1 day
    for(var _from = from; _from < to; _from += 86400000){
        //set the day
        tempDate.setTime(_from);
        //If it is a weekend add 1 to count
        if ((tempDate.getDay() <= 1) || (tempDate.getDay() >= 5)) {
            count++;
        }
    }

    //return count =)
    return count;
}