Ad Code

Add no. of days in a date to get next date(excluding weekends)

I have an date, i need to add no. of days to get future date but weekends should be excluded. i.e


input date = "9-DEC-2011";

No. of days to add = '13';

next date should be "28-Dec-2011"



Here weekends(sat/sun) are not counted.

Try this

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, count = 0;
while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
       count++;
    }
}
alert(endDate);//You can format this date as per your requirement

 

Method:2

<script>
        (function($, window, document, undefined){
        $("#fdate").on("change", function(){
          if($("#hdays").val()==""){ alert("Select how many days"); $("#fdate").val(''); d.hdays.focus(); return false; }
           var startDate = new Date($("#fdate").val()),
               noOfDaysToAdd = parseInt( ($("#hdays").val()-1) , 10),
               count = 0,
               endDate = "";

            if(!isNaN(startDate.getTime())){
                while(count < noOfDaysToAdd){
                    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
                    if(endDate.getDay() != 0 && endDate.getDay() != 6){
                       count++;
                    }
                }

                $("#tdate").val(endDate.toInputFormat());
            }else{
                alert("Invalid Date");  
            }
        });
            
        Date.prototype.toInputFormat = function(){
            var yyyy = this.getFullYear().toString();
            var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
            var dd  = this.getDate().toString();
            return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
        };
        })(jQuery, this, document);
        </script>

Method:3

 function addBusinessDaysToDate(date, days) {
  var day = date.getDay();

  date = new Date(date.getTime());
  date.setDate(date.getDate() + days + (day === 6 ? 2 : +!day) + (Math.floor((days - 1 + (day % 6 || 1)) / 5) * 2));
  return date;
}

var date = "23-JUN-2022";
var newDate = addBusinessDaysToDate(new Date(date.replace(/-/g, "/")), 3);
console.log(newDate.toString().replace(/\S+\s(\S+)\s(\d+)\s(\d+)\s.*/, '$2-$1-$3')); // alerts "28-Dec-2011"






Post a Comment

0 Comments