gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:01:33 26 4
gpt4 key购买 nike

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com