javascript – count if there are dates in array that match condition

function calculate() {
      var day = document.first.len11c.value;
      var mon = document.first.len12c.value;
      var year = document.first.len13c.value;
      var ylen = year.length;
      var m = new Array("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov",
        "Dec");
      var curd = new Date(year, mon - 1, day);
      var res2 = curd.getMonth();
      var res = new Date();
      var diff = Date.UTC(res.getFullYear(), res.getMonth() - 1, res.getDay(), 0, 0, 0) - Date.UTC(curd.getFullYear(), curd.getMonth() - 1, curd.getDay(), 0, 0, 0);
      var secleft = diff / 1000 / 60;
      var hrsleft = secleft / 60;
      var daysleft = hrsleft / 24;
      //if(res2 == (mon-1) && daysleft < 300 && year!="")
      if (year != "" && ylen > 3) {
        var fte = new Date(year, mon - 1, parseInt(day) - 45);
        document.first.fte.value = fte.getDate() + " " + m[fte.getMonth()] + " " + fte.getFullYear();
        var ste = new Date(year, mon - 1, parseInt(day) + 89);
        document.first.ste.value = ste.getDate() + " " + m[ste.getMonth()] + " " + ste.getFullYear();
        var dd = new Date(year, mon - 1, parseInt(day) + 364);
        document.first.dd.value = dd.getDate() + " " + m[dd.getMonth()] + " " + dd.getFullYear();
        var dd2 = new Date(year, mon - 1, parseInt(day) + 730);
        document.first.dd2.value = dd2.getDate() + " " + m[dd2.getMonth()] + " " + dd2.getFullYear();
        var YM1 = new Date(year, parseInt(mon - 1) - 1);
        document.first.YM1.value = YM1.getFullYear();
        var TM1 = new Date(year, parseInt(mon - 1) - 1, day = 1);
        document.first.TM1.value = m[TM1.getMonth()];


        // calculates workdays 
        /* Two working days and an sunday (not working day) */
        var startDate = new Date(year, (mon - 1) - 1, day = 1);
        var endDate = new Date(year, (mon - 1), 0);
  
        // Validate input
        if (endDate <= startDate) {
          return 0;
        }
        // Calculate days between dates
        var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
        startDate.setHours(0, 0, 0, 1); // Start just after midnight
        endDate.setHours(23, 59, 59, 999); // End just before midnight
        var diff = endDate - startDate; // Milliseconds between datetime objects    
        var days = Math.ceil(diff / millisecondsPerDay);
        // Subtract two weekend days for every week in between
        var weeks = Math.floor(days / 7);
        days -= weeks * 2;
        // Handle special cases
        var startDay = startDate.getDay();
        var endDay = endDate.getDay();
        // Remove start day if span starts on Sunday but ends before Saturday
        if (startDay == 0 && endDay != 6) {
          days--
        }
        //// Remove end day if span ends on Saturday but starts after Sunday
        if (endDay == 6 && startDay != 0) {
          days--
        }

        /* Here is the code with the calc issue */
        var daysH = 0;
        var holiday = ['2020-01-01', '2020-03-03', '2020-04-17', '2020-04-20', '2020-05-01', '2020-05-06', '2020-05-25', '2020-09-07', '2020-09-22', '2020-12-24', '2020-12-25', '2020-12-28', '2021-01-01', '2021-03-03', '2021-04-30', '2021-05-01', '2021-05-02', '2021-05-03', '2021-05-04', '2021-05-06', '2021-05-24', '2021-09-06', '2021-09-22', '2021-12-24', '2021-12-25', '2021-12-26', '2021-12-27', '2021-12-28', '2022-01-01', '2022-01-03', '2022-04-22', '2022-04-25', '2022-05-02', '2022-05-06', '2022-05-24', '2022-09-06', '2022-09-22', '2022-12-24', '2022-12-25', '2022-12-26', '2022-12-27', '2022-12-28', '2023-01-01', '2023-01-02', '2023-03-03', '2023-04-14', '2023-04-17', '2023-05-01', '2023-05-08', '2023-05-24', '2023-09-06', '2023-09-22', '2023-12-25', '2023-12-26', '2023-12-27', '2024-01-01', '2024-03-04', '2024-05-01', '2024-05-03', '2024-05-06', '2024-05-06', '2024-05-24', '2024-09-06', '2024-09-23', '2024-12-24', '2024-12-25', '2024-12-26', '2025-01-01', '2025-04-20', '2025-05-01', '2025-05-06', '2025-05-24', '2025-09-06', '2025-09-22', '2025-12-24', '2025-12-25', '2025-12-26', '2026-01-01', '2026-03-03', '2026-04-12', '2026-05-01', '2026-05-06', '2026-05-24', '2026-09-06', '2026-09-22', '2026-12-24', '2026-12-25', '2026-12-26', '2027-01-01', '2027-03-03', '2027-04-30', '2027-05-01', '2027-05-02', '2027-05-06', '2027-05-24', '2027-09-06', '2027-09-22', '2027-12-24', '2027-12-25', '2027-12-26', '2028-01-01', '2028-03-03', '2028-04-14', '2028-05-01', '2028-05-06', '2028-05-24', '2028-09-06', '2028-09-22', '2028-12-24', '2028-12-25', '2028-12-26', '2029-01-01', '2029-03-03', '2029-04-06', '2029-04-07', '2029-04-08', '2029-05-01', '2029-05-06', '2029-05-24', '2029-09-06', '2029-09-22', '2029-12-24', '2029-12-25', '2029-12-26', '2030-01-01', '2030-03-03', '2030-04-26', '2030-04-27', '2030-04-28', '2030-05-01', '2030-05-06', '2030-05-24', '2030-09-06', '2030-09-22', '2030-12-24', '2030-12-25', '2030-12-26', '2022-03-03', '2019-01-01', '2019-03-03', '2019-04-28', '2019-05-01', '2019-05-06', '2019-05-24', '2019-09-06', '2019-09-22', '2019-12-24', '2019-12-25', '2019-12-26'];
        holiday.forEach(day => {
          if ((new Date(day) >= startDate) && (new Date(day) <= endDate)); {
            if ((new Date(day).getDay() % 6) != 0);
            daysH++;
          }
        });
        var WD = (days - daysH);
        document.first.WD.value = WD;
      } else {
        alert("Date is not valid");
      }
    }

    function isNumber(evt) {
      evt = (evt) ? evt : window.event;
      var charCode = (evt.which) ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
      }
      return true;
    }
    .lefttt {
      float: left;
      color: #3D366F;
      font-size: 13px;
    }
    
    .tboxx {
      width: 45% !important;
    }
    
    .frmss {
      border: 1px solid #ddd;
      border-radius: 0.3em;
      color: #333;
      font-family: Tahoma, Geneva, sans-serif;
      font-size: 0.9em;
      line-height: 1.2em;
      margin: 0 auto;
      padding: 10px;
      width: 50%;
    }
    
    .frmss input[type="text"],
    [type="password"],
    [type="file"],
    textarea,
    select {
      background: none repeat scroll 0 0 #fff;
      border: 1px solid #ddd;
      border-radius: 0.35em;
      height: 35px;
      margin-bottom: 15px;
      margin-top: 5px;
      padding: 0 0.5%;
      width: 99%;
    }
    
    .frmss input[type="submit"],
    input[type="reset"],
    input[type="button"],
    button,
    .yellow_button,
    .blue_button {
      border: medium none;
      border-radius: 0.3em;
      color: #fff;
      cursor: pointer;
      font-weight: bold;
      margin: 10px 0;
      padding: 7px 14px;
    }
    
    .frmss input[type="reset"] {
      background: none repeat scroll 0 0 #ee765d;
      border-bottom: 3px solid #d95e44;
      text-shadow: 1px 1px 0 #8c3736;
    }
    
    input[type="button"],
    button,
    .blue_button {
      background: none repeat scroll 0 0 #468cd2;
      border-bottom: 3px solid #3277bc;
      text-shadow: 1px 1px 0 #214d73;
    }
    
    .new {
      padding: 16px;
    }
    
    @media only screen and (max-width:480px) {
      .frmss {
        width: 90% !important;
      }
      .tboxx {
        width: 95% !Important;
      }
      .frmss input[type="text"],
      [type="password"],
      [type="file"],
      textarea,
      select {
        width: 95% !important;
      }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script type="text/JavaScript" src=" https://MomentJS.com/downloads/moment.js"></script>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

  <div class="resp_code2">
    <p align='center'>
      <h2 align='center'>Dates Calculator
      </h2>
    </p>
    <div class="clear"></div>
    <form id='tab' name=first class="frmss">
      <div>
        <div align='center'><label>Enter due date:</label></div><br>
        <div class="group_con2">
          <label class="lefttt">day: </label>
          <select name=len11c>
            <option name=1 value=1>1</option>
            <option name=2 value=2>2</option>
            <option name=3 value=3>3</option>
            <option name=4 value=4>4</option>
            <option name=5 value=5>5</option>
            <option name=6 value=6>6</option>
            <option name=7 value=7>7</option>
            <option name=8 value=8>8</option>
            <option name=9 value=9>9</option>
            <option name=10 value=10>10</option>
            <option name=11 value=11>11</option>
            <option name=12 value=12>12</option>
            <option name=13 value=13>13</option>
            <option name=14 value=14>14</option>
            <option name=15 value=15>15</option>
            <option name=16 value=16>16</option>
            <option name=17 value=17>17</option>
            <option name=18 value=18>18</option>
            <option name=19 value=19>19</option>
            <option name=20 value=20>20</option>
            <option name=21 value=21>21</option>
            <option name=22 value=22>22</option>
            <option name=23 value=23>23</option>
            <option name=24 value=24>24</option>
            <option name=25 value=25>25</option>
            <option name=26 value=26>26</option>
            <option name=27 value=27>27</option>
            <option name=28 value=28>28</option>
            <option name=29 value=29>29</option>
            <option name=30 value=30>30</option>
            <option name=31 value=31>31</option>
          </select>
        </div>
        <div class="group_con2"><label class="lefttt">Month:</label>
          <select name=len12c>
            <option name=1 value=1>Jan</option>
            <option name=2 value=2>Feb</option>
            <option name=3 value=3>March</option>
            <option name=4 value=4>April</option>
            <option name=5 value=5>May</option>
            <option name=6 value=6>June</option>
            <option name=7 value=7>July</option>
            <option name=8 value=8>Aug</option>
            <option name=9 value=9>Sept</option>
            <option name=10 value=10>Oct</option>
            <option name=11 value=11>NOV</option>
            <option name=12 value=12>Dec</option>
          </select>
        </div>
        <div class="group_con2"><label class="lefttt">Year:</label>
          <input name=len13c type=text onkeypress="return isNumber(event)" maxlength="4">
        </div>
      </div>
      <div align='center'>
        <input type=button name=cc2 value=Calculate onClick="calculate()">
        <input type="reset" value="Clear">
      </div>
      <div align='center'>
        <h5></h5>
      </div>

      <div> <label class="lefttt">1st day of sick leave :</label>
        <input type=text readonly name=fte> </div>
      <div> <label class="lefttt">last day of sick leave:</label>
        <input type=text readonly name=ste> </div>
      <div> <label class="lefttt"> 1st year maternity end date :</label>
        <input type=text readonly name=dd> </div>
      <div> <label class="lefttt">2nd year maternity end date  :</label>
        <input type=text readonly name=dd2> </div>
      <script type="text/javascript"></script>



      <title>PASTCALCDAY</title>

      <style>
        teble {
          width: 400px;
        }
        
        th,
        caption {
          text-align: left;
        }
        
        table,
        th,
        td {
          border: 1px solid #333;
          border-collapse: collapse;
        }
        
        th,
        td {
          padding: 10px;
        }
        
        tr:nth-child(even) {
          background-color: #FBEAFF;
        }
        
        tr:nth-child(odd) {
          background-color: #B39CD0;
        }
      </style>


      <script type="text/javascript">
      </script>

      <table>
        <caption> Calculating daily compensation</caption>
        <caption> (Needed data 24 moths before 1st day of sick leave </caption>

        <tr>
          <th>year</th>
          <th>month</th>
          <th>Working days (without Holidays)</th>
        </tr>
        <td> <input type=text readonly name=YM1> </td>
        <td> <input type=text readonly name=TM1> </td>
        <td> <input type=text readonly name=WD> </td>


      </table>
    </form>
</body>

</html>

Leave a Comment