How to determine the number of Saturdays and Sundays occurs between two dates in a java script

I have a requirement as follows I have two dates that I need to find how Saturdays and Sundays can appear Date1: 02/06/2011
Date2: 02/07/2011
10 days - days off
thanks to Srini

+5
source share
5 answers

O (1) without 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

+6
source

Edited to calculate the number of days off instead of the number of days off. 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)));
+4
source

( , 10 ). - ...

var chunks = str.split('/');
str = [chunks[1], chunks[0], chunks[2]].join('/');

, - .

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.

+1

: 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>
0

, , , OP 2 2011 2 2011 , 10 , : 02:06, 03:06, 09: 06, 10:06, 16:06, 17:06, 23:06, 24:06, 30:06, 31:06.

A 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);

Please note that this function may not work as you expect if you use it in the server settings (for example, Node.js), because if you do not specify the UTC time zone, it can be translated to your local time and will come down for one day, which can lead to incorrect results.

0
source

All Articles