javascript 如何确定星期六和星期日在java脚本中两个日期之间的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6210906/
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
How to determine number Saturdays and Sundays comes between two dates in java script
提问by Srini
I have requirement as follows
I have two dates i need to find how may saturdays and sundays will come in between
Date1: 02/06/2011
Date2: 02/07/2011
10 days are weekends
Thanks
Srini
我的要求如下 我有两个日期 我需要找出星期六和
星期日之间如何出现Date1: 02/06/2011
Date2: 02/07/2011
10 天是周末
谢谢 Srini
回答by Matt
回答by Brett
Edited to count number of weekend days instead of number of weekends. http://jsfiddle.net/bRgUq/3/
编辑以计算周末天数而不是周末数。http://jsfiddle.net/bRgUq/3/
function CalculateWeekendDays(fromDate, toDate){
var weekendDayCount = 0;
while(fromDate < toDate){
fromDate.setDate(fromDate.getDate() + 1);
if(fromDate.getDay() === 0 || fromDate.getDay() == 6){
++weekendDayCount ;
}
}
return weekendDayCount ;
}
console.log(CalculateWeekendDays(new Date(2011, 6, 2), new Date(2011, 7, 2)));
回答by alex
According to your dates, they are not in US format (at least not if there are 10 weekend days between them). You can get them in US format with something such as...
根据您的日期,它们不是美国格式(至少如果它们之间有 10 个周末天,则不是)。您可以使用美国格式获取它们,例如...
var chunks = str.split('/');
str = [chunks[1], chunks[0], chunks[2]].join('/');
This code loops through each day between the dates and increments a counter if the day is a Saturday or Sunday.
此代码循环遍历日期之间的每一天,如果当天是星期六或星期日,则增加一个计数器。
var start = new Date('06/02/2011'),
finish = new Date('07/02/2011'),
dayMilliseconds = 1000 * 60 * 60 * 24;
var weekendDays = 0;
while (start <= finish) {
var day = start.getDay()
if (day == 0 || day == 6) {
weekendDays++;
}
start = new Date(+start + dayMilliseconds);
}
回答by mplungjan
Brute force: http://jsfiddle.net/mplungjan/vwNfU/
蛮力:http: //jsfiddle.net/mplungjan/vwNfU/
<script>
var aDay = 24*60*60*1000;
function getWeekend(dString1,dString2) {
var d1 = new Date(Date.parse(dString1)); //"MM/DD/YYYY"
var d2 = new Date(Date.parse(dString2));
var weekend = {
Sat:0,
Sun:0
}
for (var d,i=d1.getTime(), n=d2.getTime();i<=n;i+=aDay) {
d=new Date(i).getDay();
document.write("<br>"+new Date(i)+":"+d);
if (d===6) weekend.Sat++;
if (d===0) weekend.Sun++;
}
return weekend;
}
var satsun = getWeekend("06/02/2011","07/02/2011")
document.write("<br>Sat:"+satsun.Sat+"\nSun:"+satsun.Sun)
</script>
回答by mplungjan
I will make a wild guess and say that probably OP meant the interval between July 2, 2011 and August 2, 2011, in which case it is indeed 10 weekends, exactly these: 02:06, 03:06, 09:06, 10:06, 16:06, 17:06, 23:06, 24:06, 30:06, 31:06.
我会做一个疯狂的猜测并说可能OP意味着2011年7月2日和2011年8月2日之间的间隔,在这种情况下确实是10个周末,正是这些:02:06, 03:06, 09:06, 10 :06, 16:06, 17:06, 23:06, 24:06, 30:06, 31:06。
The way to calculate this, without a loop:
计算这个的方法,没有循环:
function weekendsBetween(start, end) {
"use strict";
var startDay = start.getDay(),
diff = (end.getTime() - start.getTime() - startDay) / (60000 * 60 * 24),
diffWeaks = (diff / 7) | 0,
remWeaks = Math.ceil(diff % 7), extra = 0;
if (startDay + remWeaks > 7) extra = 2;
else if (startDay + remWeaks == 7 ||
remWeaks > startDay) extra = 1;
return diffWeaks * 2 + extra;
}
var date1 = new Date(2011, 6, 2);
var date2 = new Date(2011, 7, 2);
weekendsBetween(date1, date2);
Note that this function may not act as you expect it to if you use it in the server settings (eg. Node.js), because if you don't specify UTC time zone, it may get translated into your local time and will get off by one day, which may cause incorrect results.
请注意,如果您在服务器设置(例如 Node.js)中使用此函数,它可能不会像您期望的那样运行,因为如果您不指定 UTC 时区,它可能会被转换为您的本地时间并会得到关闭一天,这可能会导致错误的结果。