How to determine number Saturdays and Sundays comes between two dates in java script

后端 未结 5 526
余生分开走
余生分开走 2020-12-06 13:32

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

相关标签:
5条回答
  • 2020-12-06 14:12

    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.

    0 讨论(0)
  • 2020-12-06 14:13

    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>
    
    0 讨论(0)
  • 2020-12-06 14:13

    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.

    0 讨论(0)
  • 2020-12-06 14:23

    O(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

    0 讨论(0)
  • 2020-12-06 14:37

    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)));
    
    0 讨论(0)
提交回复
热议问题