0

I am stuck in between with my problem of "How to calculate the days between the dates excluding the Weekends(SAT,SUN) and the National Holidays." and really struggling to calculate the weekends and nationaldays holidays both as the same time.

Any Help would be severely appreciated.

I have arrived to the conclusion in my code , that i am getting the calculated days where i am able to exclude the weekends , but not the national holidays, I have tried to built but failed. Please let me know how to calculate both the weekends and national holidays simultaneously.

This is my html code:

<label>Leave From</label>
                <input name="from" id="from" type="text" class="form-control" value="" / >
              </div>
              <div class="form-group" id="to_date">
                <label>To</label>
                <input name="to" id="to" type="text" class="form-control" >
              </div>
              <input type="text" id="hasil" name="hasil" readonly />

This is my script:

<script>
  var disabledDays = ["3-24-2015", "3-25-2015"];

   function nationalDays(date) {
       var m = date.getMonth(),
           d = date.getDate(),
           y = date.getFullYear();

       for (i = 0; i < disabledDays.length; i++) {
           if ($.inArray((m + 1) + '-' + d + '-' + y, disabledDays) != -1 || new Date() > date) {
               return [false];
           }
       }
       return [true];
   }

   function noWeekendsOrHolidays(date) {
       var noWeekend = jQuery.datepicker.noWeekends(date);
       return noWeekend[0] ? nationalDays(date) : noWeekend;
   }

   $(function () {
       $("#from").datepicker({
           minDate:0,
           changeMonth: true,
           constrainInput: true,
           numberOfMonths: 1,
           beforeShowDay: $.datepicker.noWeekends,
           onSelect: calculateDays,
           onClose: function (selectedDate) {
               var day = $("#from").datepicker('getDate');
               $("#to").datepicker("option", "minDate", selectedDate);
           },
       }).on("changeDate",function(ev){
      var fromdate = new Date(ev.date);
      fromdate.setMonth(fromdate.getMonth()+1);

      var finaldate = fromdate.getFullYear()+"-"+fromdate.getMonth()+"-"+fromdate.getDate();
      console.log(finaldate);
      $("#fromdate").val(finaldate);
   });

     });

   $(function () {
       $("#to").datepicker({
          minDate:0,
         beforeShowDay: $.datepicker.noWeekends,
           changeMonth: true,
           constrainInput: true,
           numberOfMonths: 1,
           onSelect: calculateDays,
           onClose: function (selectedDate) {
               $("#from").datepicker("option", "maxDate", selectedDate);
           },
       }).on("changeDate",function(ev){
      var todate = new Date(ev.date);
      todate.setMonth(todate.getMonth()+1);

      var finaldate = todate.getFullYear()+"-"+todate.getMonth()+"-"+todate.getDate();
      console.log(finaldate);
      $("#todate").val(finaldate);
   });

   });

   function calculateDays(startDate, endDate) {
       var form = this.form

       var startDate = document.getElementById("from").value;
       var startDate = new Date(startDate);

       var endDate = document.getElementById("to").value;
       var endDate = new Date(endDate);

       startDate.setHours(0, 0, 0, 1); // Start just after midnight
       endDate.setHours(23, 59, 59, 999); // End just before midnight

       var oneDay = 24 * 60 * 60 * 1000;
       var diff = endDate - startDate; // Milliseconds between datetime objects
       var days = Math.ceil(diff / oneDay);

       var weeks = Math.floor(days / 7);
       var days = days - (weeks * 2);

       // Handle special cases
       var startDay = startDate.getDay();
       var endDay = endDate.getDay();

       // Remove weekend not previously removed.
       if (endDate < startDate) {
           return 0;
       }

       if (startDay - endDay > 1) days = days - 2;




       if (days) document.getElementById("hasil").innerHTML = days;
       $("#hasil").val(days);

   }


</script>

Here is the link to jsfiddle: https://jsfiddle.net/8ww4noja/2/

Jay Desai
  • 475
  • 3
  • 10
  • 27
  • This might help http://stackoverflow.com/a/503082/3639582. – Shaunak D Mar 27 '15 at 06:07
  • I have updated my jsfiddle, and got the national holidays disabled, but cannot calculate the days excluding the dates of national holidays. http://jsfiddle.net/8ww4noja/4/ – Jay Desai Mar 27 '15 at 06:24
  • @ShaunakD hey dude, i tried this one, but could not manage to calculate the today days, can u please help me out? – Jay Desai Mar 27 '15 at 07:21

0 Answers0