使用 Javascript 获取工作日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7952239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Working Days Using Javascript
提问by hawx
I'm trying to get the working days between two date. Example: stdate = 28/10/2011 and endate = 04/11/2011. This should be 6 working days, but its only giving 5 days.
我正在尝试获取两个日期之间的工作日。示例:stdate = 28/10/2011 和 endate = 04/11/2011。这应该是 6 个工作日,但它只给了 5 天。
var workingdays = 0;
var weekday = new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
while (stdate <= endate)
{
var day = weekday[stdate.getDay()];
if(day != "Saturday" && day != "Sunday")
{
workingdays++;
}
console.log(weekday[stdate.getDay()]);
stdate = new Date(stdate.getTime() + 86400000);
}
The console log shows the results below.
控制台日志显示以下结果。
Friday
Saturday
Sunday
Sunday
Monday
Tuesday
Wednesday
Thursday
Sunday shows twice for some reason. Any help would be appreciated.
出于某种原因,周日播放了两次。任何帮助,将不胜感激。
采纳答案by Guffa
Daylight savings time.
夏令时。
As you are adding 24 hours to the date, it's not enough to get you to the next day on the sunday, as that particular sunday has 25 hours.
当您在日期上增加 24 小时时,仅仅将您带到星期日的第二天是不够的,因为那个特定的星期天有 25 小时。
You should add a day instead of adding hours:
您应该添加一天而不是添加小时:
stdate = new Date(stdate.getFullYear(), stdate.getMonth(), stdate.getDate() + 1);
Explanation: When you call the Date
constructor with a date that is out of range, it will automaticall wrap to the next month, i.e. new Date(2010,9,32)
gives you the first of November.
说明:当您Date
使用超出范围的日期调用构造函数时,它会自动换行到下个月,即new Date(2010,9,32)
给您 11 月的第一天。
回答by Bas Slagter
Here you go:
干得好:
function getWorkingDays(startDate, endDate){
var result = 0;
var currentDate = startDate;
while (currentDate <= endDate) {
var weekDay = currentDate.getDay();
if(weekDay != 0 && weekDay != 6)
result++;
currentDate.setDate(currentDate.getDate()+1);
}
return result;
}
var begin = new Date(2011, 09, 8);
var end = new Date(2011, 09, 25);
alert(getWorkingDays(begin, end)); // result = 12 days
Keep in mind that the month indication for the two variables is zero based. So in my example we are looking at october (month 10).
请记住,两个变量的月份指示是从零开始的。因此,在我的示例中,我们正在查看 10 月(第 10 个月)。
回答by Ankur
You can try this
你可以试试这个
stdate.setDate(stdate.getDate()+1);
in place of
代替
stdate = new Date(stdate.getTime() + 86400000);
回答by Binh LE
You can get Date of week from StartDate and EndDate
您可以从 StartDate 和 EndDate 获取星期几
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var object = [];
function getTuesday(startDate, endDate, n) {
var currentDate = startDate;
while (currentDate <= endDate) {
if (weekday[currentDate.getDay()] === weekday[n])
object.push(currentDate + "\n");
currentDate.setDate(currentDate.getDate() + 1);
} return object;
}
var begin = new Date(2015, 11, 08);
var end = new Date(2016, 01, 08);
alert(getTuesday(begin, end, 3) + "\n Total date or week: " + object.length);