admin管理员组文章数量:1194940
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
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
- 2 Please show us some solutions you've tried so we have something to work against. – Khepri Commented Jun 2, 2011 at 5:49
- 2 find first saturday with ...getDay(), then plus 7 – Ibu Commented Jun 2, 2011 at 5:50
- 2 There are only 9 weekend days between (inclusive) those 2 dates – Petah Commented Jun 2, 2011 at 6:20
5 Answers
Reset to default 14O(1) solution with no loops:
function countWeekendDays( d0, d1 )
{
var ndays = 1 + Math.round((d1.getTime()-d0.getTime())/(24*3600*1000));
var nsaturdays = Math.floor( (d0.getDay()+ndays) / 7 );
return 2*nsaturdays + (d0.getDay()==0) - (d1.getDay()==6);
}
jsFiddle
Edited to count number of weekend days instead of number of weekends. 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)));
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...
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);
}
jsFiddle.
Brute force: 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>
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.
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.
本文标签:
版权声明:本文标题:javascript - How to determine number Saturdays and Sundays comes between two dates in java script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738512515a2090891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论