我有两个日期。一个是开始日期,另一个是结束日期。我想计算日期范围内有多少星期六,星期一和星期三?我该如何解决它?我看了几个教程,但他们只计算日期范围内的日期。提前致谢。我使用下面的代码来计算只有工作日,但我只需要在日期范围内有多少星期六,星期一和星期三。Javascript日期范围内有多少个特定日期
<!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>
你尝试过什么吗? –
请提供您尝试展示一些功能的示例。我们在这里帮助您,而不是为您编写所有代码。谢谢 –
如果你已经有单日期的范围,你可以迭代它们并筛选出你感兴趣的所有工作日。为了得到一周的某一天,你可以使用[Date.getDay()](http ://www.w3schools.com/jsref/jsref_getday.asp) – jhohlfeld