skip to Main Content

I am trying to compute number of days between a range of dates input by the user using date picker using the following script:

 $(document).ready(function () {
            
            $('#datepicker1').datepicker();
            $('#datepicker2').datepicker();
            $('#datepicker3').datepicker();
            $('#datepicker4').datepicker();
            
            $(function() {
                let $fromDate = $('#fromdate'),
                    $toDate = $('#todate'),
                    $numberDays = $('#leavedays'),
                    $sfromDate=$('#sfromdate'),
                    $stoDate=$('#stodate'),
                    $snumberDays=$('#sleavedays');
                    

                $fromDate.datepicker().on('change', function(){
                    $toDate.datepicker('option', 'minDate', $(this).val());
                    $numberDays.val(calculateDateDiff($toDate.val(), $(this).val())+1);
                });

                $toDate.datepicker().on('change', function(){
                    $fromDate.datepicker('option', 'maxDate', $(this).val());
                    $numberDays.val(calculateDateDiff($(this).val(), $fromDate.val())+1);
                });

                $sfromDate.datepicker().on('change', function(){
                    $stoDate.datepicker('option', 'minDate', $(this).val());
                    $snumberDays.val(calculateDateDiff($stoDate.val(), $(this).val())+1);
                });

                $stoDate.datepicker().on('change', function(){
                    $sfromDate.datepicker('option', 'maxDate', $(this).val());

                    $snumberDays.val(calculateDateDiff($(this).val(), $sfromDate.val())+1);
                });

                function calculateDateDiff(endDate, startDate) {
                        

                    if (endDate && startDate) {
                        let e = moment(endDate),
                            s = moment(startDate);

                        return e.diff(s, "days");
                    }

                    return null;
                }
            });
         });

The script is able to return the correct difference between from date and to date, but I want to exclude weekend while computing the difference. Please help how to proceed for the same

2

Answers


  1. An easy way is to adjust the input arguments to be the first/last day of the week, then divide by 7 to determine how many weekends in between those dates that you need to remove:

    Fiddle: https://jsfiddle.net/k3uxeo12/3/

    function calculateDateDiff(endDate, startDate) {
    
        if (endDate && startDate) {
            let e = moment(endDate),
                s = moment(startDate);
    
            let days = e.diff(s, "days") + 1; // diff only counts the boundary, so add 1
            
            // .isoWeekday() returns 1-7 where 1 is Monday and 7 is Sunday
            // Adjust the end to the Friday before, if it is a weekend
            if (e.isoWeekday() > 5)
                days -= e.isoWeekday() % 5; // will be 1 for Saturday, 2 for Sunday
            
            // adjust the start to the next Monday, if it is a weekend
            if (s.isoWeekday() > 5)
                days -= (3 - (s.isoWeekday() % 5)); // will be 2 for Saturday, 1 for Sunday
    
            if (days > 5) {
                // remove the weekends from each full week
                let weeks = (days - (days % 7)) / 7;
                days -= (weeks * 2);
            }
            
            return days;
        }
    
        return null;
    }
    

    Having a quick search you can find other very similar posts:

    Login or Signup to reply.
  2. Not DatePicker related but in terms of time, I only get the value of the date and send it to the server. This is what I did using PHP,

    function getDatesFromRange($start, $end, $format = 'Ymd') {
    $array = array();
    $day = 'D';
    $interval = new DateInterval('P1D');
    
    $realEnd = new DateTime($end);
    $realEnd->add($interval);
    
    $period = new DatePeriod(new DateTime($start), $interval, $realEnd);
    
    foreach($period as $date) { 
        $day_name = $date->format($day);
        $date = $date->format($format);
    
        $array[] = array(
            'date' => $date,
            'day' => $day_name,
            'duration' => $day_name == 'Sun' ? "It's weekend!" : "Not a weekend.",
        );
    }
    return $array;} echo "<pre>"; print_r(getDatesFromRange('20221125', '20221224')); echo "</pre>";
    

    I found the method somewhere and modified it a little bit. It will output the following:

    (
    [0] => Array
        (
            [date] => 20221125
            [day] => Fri
            [duration] => Not a weekend.
        )
    
    [1] => Array
        (
            [date] => 20221126
            [day] => Sat
            [duration] => Not a weekend.
        )
    
    [2] => Array
        (
            [date] => 20221127
            [day] => Sun
            [duration] => It's weekend!
        )
    
    [3] => Array
        (
            [date] => 20221128
            [day] => Mon
            [duration] => Not a weekend.
        )
    

    …and so on. I think it’s easier to apply only restrictions to the frontend but the real logic was on the backend… sorry for my english

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search