在Javascript中,一个日期范围内的具体天数是多少

在Javascript中,一个日期范围内的具体天数是多少,javascript,timespan,dayofweek,Javascript,Timespan,Dayofweek,我有两次约会。一个是开始日期,另一个是结束日期。我想计算有多少星期六、星期一和星期三在日期范围内?我怎样才能解决它?我看了几个教程,但他们只计算日期范围内的日期。提前谢谢。我使用以下代码只计算工作日,但我只需要多少星期六,星期一和星期三在日期范围内 <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> <!--[if IE]

我有两次约会。一个是开始日期,另一个是结束日期。我想计算有多少星期六、星期一和星期三在日期范围内?我怎样才能解决它?我看了几个教程,但他们只计算日期范围内的日期。提前谢谢。我使用以下代码只计算工作日,但我只需要多少星期六,星期一和星期三在日期范围内

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
  <script>
        function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
        var iWeeks, iDateDiff, iAdjust = 0;
        if (dDate2 < dDate1) return -1; // error code if dates transposed
        var iWeekday1 = dDate1.getDay(); // day of week
        var iWeekday2 = dDate2.getDay();
        iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
        iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
        if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
        iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
        iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

        // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
        iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

        if (iWeekday1 <= iWeekday2) {
          iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
        } else {
          iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
        }

        iDateDiff -= iAdjust // take into account both days on weekend

        return (iDateDiff + 1); // add 1 because dates are inclusive
    }
    </script>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <script>
    alert(calcBusinessDays(new Date("August 01, 2010 11:13:00"),new Date("August 31, 2010 11:13:00")));
  </script>
</body>
</html>
// days is an array of weekdays: 0 is Sunday, ..., 6 is Saturday
function countCertainDays( days, d0, d1 ) {
  var ndays = 1 + Math.round((d1-d0)/(24*3600*1000));
  var sum = function(a,b) {
    return a + Math.floor( (ndays+(d0.getDay()+6-b) % 7 ) / 7 ); };
  return days.reduce(sum,0);
}

JS-Bin
函数calcBusinessDays(dDate1,dDate2){//作为日期对象提供的输入
var iWeeks、iDateDiff、Idejust=0;
if(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我的方法:

首先,从两个日期之间的范围获取日期列表:

function getDates(dateStart, dateEnd) {
  var currentDate = dateStart,
      dates = [];
  while(currentDate <= dateEnd) {

    // append date to array
    dates.push(currentDate);

    // add one day
    // automatically rolling over to next month
    var d = new Date(currentDate.valueOf());
    d.setDate(d.getDate() + 1);
    currentDate = d;

  }
  return dates;
}
筛选选项:

周日为0,周一为1,太阳、周一、周三的列表为:
[0、1、3]

JS Bin:

O(1)解决方案。循环一周中的几天(不超过7天),而不是日期范围

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
  <script>
        function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
        var iWeeks, iDateDiff, iAdjust = 0;
        if (dDate2 < dDate1) return -1; // error code if dates transposed
        var iWeekday1 = dDate1.getDay(); // day of week
        var iWeekday2 = dDate2.getDay();
        iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
        iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
        if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
        iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
        iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

        // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
        iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

        if (iWeekday1 <= iWeekday2) {
          iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
        } else {
          iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
        }

        iDateDiff -= iAdjust // take into account both days on weekend

        return (iDateDiff + 1); // add 1 because dates are inclusive
    }
    </script>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <script>
    alert(calcBusinessDays(new Date("August 01, 2010 11:13:00"),new Date("August 31, 2010 11:13:00")));
  </script>
</body>
</html>
// days is an array of weekdays: 0 is Sunday, ..., 6 is Saturday
function countCertainDays( days, d0, d1 ) {
  var ndays = 1 + Math.round((d1-d0)/(24*3600*1000));
  var sum = function(a,b) {
    return a + Math.floor( (ndays+(d0.getDay()+6-b) % 7 ) / 7 ); };
  return days.reduce(sum,0);
}
例如,计算2014年1月的周一(1)、周三(3)和周六(6):

countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,1)) // 1
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,2)) // 1
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,3)) // 1
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,4)) // 2
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,5)) // 2
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,6)) // 3
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,7)) // 3
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,8)) // 4
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,9)) // 4
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,10)) // 4
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,11)) // 5

注意:这假设
d0
d1
Date
对象,它们的时间大致相同。如果您创建的
Date
对象仅指定年、月和日,则没有问题。

您尝试过什么吗?请提供一个您尝试展示一些努力的示例。我们在这里帮助I’我不会为你写所有的代码。谢谢如果你已经有了单一日期的范围,你可以迭代它们并过滤掉所有你感兴趣的工作日。为了得到一周中的哪一天,你可以使用我已经按照你的建议更新了我的问题。非常感谢。我已经检查了代码。工作正常。我认为只有ne问题。不计算最后日期。例如,如果结束日期在工作日内,则返回的结果比实际结果少1天。在2014年1月1日至2014年1月31日,如果工作日为周六、周一和周三,则返回的结果为13个工作日,而如果我将2014年1月29日作为结束日期,也就是周三,则返回的结果为13个工作日s返回12个工作日,应该是13个。我更改了答案。看一下while语句。它现在应该包括endDate。这非常有用。有没有公式可以让我也给出一个日期数组,它将被排除在两个日期之间,或者不计算在两个日期之间?