Javascript 日期范围内的特定天数 [英] How many specific days within a date range in Javascript
问题描述
我有两个约会.一个是开始日期,另一个是结束日期.我想计算日期范围内有多少个星期六、星期一和星期三?我该如何解决?我看过几个教程,但他们只计算日期范围内的日期.提前致谢.我使用以下代码仅计算工作日,但我只需要在日期范围内有多少个星期六、星期一和星期三.
<头><元字符集=utf-8/><title>JS Bin</title><!--[如果 IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--><脚本>function calcBusinessDays(dDate1, dDate2) {//作为日期对象给出的输入var iWeeks, iDateDiff, iAdjust = 0;如果 (dDate2 < dDate1) 返回 -1;//日期转置时的错误代码var iWeekday1 = dDate1.getDay();//星期几var iWeekday2 = dDate2.getDay();iWeekday1 = (iWeekday1 == 0) ?7 : iWeekday1;//将星期日从 0 更改为 7iWeekday2 = (iWeekday2 == 0) ?7 : iWeekday2;if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;//周末两天调整iWeekday1 = (iWeekday1 > 5) ?5 : iWeekday1;//只计算工作日iWeekday2 = (iWeekday2 > 5) ?5 : iWeekday2;//以周为单位计算差异(1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime())/604800000)如果 (iWeekday1 <= iWeekday2) {iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)} 别的 {iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)}iDateDiff -= iAdjust//考虑周末的两天返回 (iDateDiff + 1);//加 1 因为日期包含在内}<风格>文章,旁边,图,页脚,页眉,hgroup,菜单,导航,部分 { 显示:块;}</风格>头部><身体><脚本>alert(calcBusinessDays(new Date("August 01, 2010 11:13:00"),new Date("August 31, 2010 11:13:00")));</html>
我的方法:
首先,从两个日期之间的范围中获取日期列表:
function getDates(dateStart, dateEnd) {var currentDate = dateStart,日期 = [];while(currentDate <= dateEnd) {//将日期附加到数组日期.推(当前日期);//加一天//自动滚动到下个月var d = new Date(currentDate.valueOf());d.setDate(d.getDate() + 1);当前日期 = d;}返回日期;}
然后循环浏览这些日期,过滤出相关的工作日索引:
function filterWeekDays(dates, includeDays) {var 工作日 = [];//循环日期日期.forEach(功能(天){//要包含的周期天数(so==0、mo==1 等)includeDays.forEach(function(include) {if(day.getDay() == 包括) {工作日推送(天);}});});平日返回;}
过滤选项:
当星期日为 0 且星期一为 1 时,Sun、Mon、Wed 的列表将是:[0, 1, 3]
JS Bin:http://jsbin.com/furupoqu/5/edit>
I have two dates. One is starting date and another is ending date. I want to calculate how many Saturdays, Mondays and Wednesdays falls within the date range? How can I solve it? I saw several tutorial but they are only counting the dates within date range. Thanks in advance. I am using the following code to calculate only business days but I need only how many saturdays, mondays and wednesdays falls within the date range.
<!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>
My approach:
First, acquire a list of dates from a range between two dates:
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;
}
Then cycle through those dates, filtering for a relevant work day index:
function filterWeekDays(dates, includeDays) {
var weekdays = [];
// cycle dates
dates.forEach(function(day){
// cycle days to be included (so==0, mo==1, etc.)
includeDays.forEach(function(include) {
if(day.getDay() == include) {
weekdays.push(day);
}
});
});
return weekdays;
}
Filtering options:
With Sunday beeing 0 and Monday being 1, a list of Sun, Mon, Wed would be: [0, 1, 3]
JS Bin: http://jsbin.com/furupoqu/5/edit
这篇关于Javascript 日期范围内的特定天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!