javascript - Javascript 中日期范围内的特定天数

标签 javascript timespan dayofweek

我有两个约会对象。一个是开始日期,另一个是结束日期。我想计算日期范围内有多少个星期六、星期一和星期三?我该如何解决?我看过几个教程,但他们只计算日期范围内的日期。提前致谢。我使用以下代码仅计算工作日,但我只需要日期范围内有多少星期六、星期一和星期三。

<!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>

最佳答案

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

// 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

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

关于javascript - Javascript 中日期范围内的特定天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985259/

相关文章:

javascript - Canvas 混合不起作用

javascript - Rails 中带有 '/' 和 '.' 字符的 select_tag 的 OnChange 问题

javascript - 使用 Enzymejs 测试渲染子组件

r - R 中的第 53 周?

java - 日期计算器 : Tell what day of the week a certain date is

Postgresql 星期几的 Java 常量

javascript - JSP点击按钮弹出窗口的方法

c# - 如何将 12 小时时间字符串转换为 C# TimeSpan?

c# - 在 C# 中使用时间跨度或表示时间跨度的整数值之间的区别

C# TimeSpan.Milliseconds格式为2位