1

I am using this javascript to get two datepickers using the following code:

$(document).ready(function() {
   $("#astartdate").datepicker();
   $("#aenddate").datepicker();
});

I want to calculate the number of the days between the dates without weekend dates and alert the count dates.

  • 5/7th of the date difference – Jaromanda X Jan 25 '16 at 05:57
  • Check this SO thread [http://stackoverflow.com/questions/20788411/how-to-exclude-weekends-between-two-dates-using-moment-js] answer by acoiro – Satyaki Chatterjee Jan 25 '16 at 06:21
  • 1
    Possible duplicate of [Find day difference between two dates (excluding weekend days)](http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days) – Mosh Feu Jan 25 '16 at 06:44
  • i review answer but i want to how to write 'javascript' calculate date different without weekend .If i select 12 days it must have 2 or 4 days in weekend – Isuru Udapadi Jan 25 '16 at 06:45

2 Answers2

1
var SUNDAY = 0;
var SATURDAY = 6;

Date.prototype.isWeekDay = function () {
    return this.getDay() != SATURDAY && this.getDay() != SUNDAY;
}

function getWorkingDaysBetween(startDate, endDate) {
    var d = startDate;
    var result = 0;
    while (d <= endDate) {
        if (d.isWeekDay())
            result++;
        d = d.addDays(1);
    }
    return result;
}
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
1
function getDates($startTime, $endTime) {
  $day = 86400;
  $format = 'Y-m-d';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
//$numDays = round(($endTime - $startTime) / $day) + 1;
$numDays = round(($endTime - $startTime) / $day); // remove increment 

$days = array();

    for ($i = 0; $i < $numDays; $i++) { //change $i to 1
    $days[] = date($format, ($startTime + ($i * $day)));
  }
return $days;

}

$date=array();
        $date=getDates($stCodate, $enCodate);
        //print_r( $date);
        $cntdate="0";
        foreach($date as $day ){
            echo date('D', strtotime( $day));
            $nmday=date('D', strtotime( $day));

            if(!($nmday=="Sat"||$nmday=="Sun")){
                $cntdate++;

            }
            //echo $cntdate;
        }