0

i want to calculate the range between 2 dates without counting weekend in javascript. i have some code that already count the range between them. but i'm stuck with the weekend part. date inputed by CJuiDatePicker in YII framework

    <script>
    function calcDay(dt1, dt2, range){

    var msec1 = dt1;
    var date1 = new date(msec1);
    var msec2 = dt2;
    var date2 = new date(msec2);

    if(date1>0 || date2>0){
    range.val(isFinite(Math.round(date2-date1)/86400000) || 0);
    }
    };
    </script>

86400000 is day in millisecond

thanks in advance

Trainee
  • 15
  • 7
  • 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) – George Mar 16 '17 at 15:03
  • thanks for replying, i have tried it before but still got no idea. – Trainee Mar 16 '17 at 15:16

2 Answers2

0

The function you'll need is getUTCDay().

the pseudo code would be as follows:
1 - determine full weeks (days/7 truncated)
2 - calculate weekday/weekend: 2 * result = weekend days, 5 * result = weekdays.
3 - after that, remainder and starting day of week will determine the 1 or 2 additional days

Hope that helps, let me know if you need the javascript, - John

Edited, as requested. NOTE: tweaked your original for testing, you should spot the needed changes to restore.

function calcDay(dt1, dt2, range)
{
    var msec1 = "October 13, 2014 11:13:00";
    var date1 = new Date(msec1);
    var msec2 = "October 13, 2013 11:13:00";
    var date2 = new Date(msec2);

    var days;
    var wdays;
    var startday;
    var nLeft;

    // neither should be zero
    if(date1>0 && date2>0) {
        days = Math.round( Math.abs((date2-date1)/86400000) );
        wdays = Math.round(days / 7) * 5;
        nLeft = days % 7;
        startday = (date1 > date2) ? date2.getUTCDay() : date1.getUTCDay();

        if (startday < 2) {
            wdays += Math.max(nLeft+startday-1,0);
        } else if (startday == 6) {
            wdays += Math.max(nLeft-2,0);
        } else if (nLeft > (7-startday)) {
            wdays += (nLeft-2)
        } else {
            wdays += Math.min(nLeft, 6-startday)
        }
    }
};
John
  • 182
  • 7
  • Can i have the javascript? Cz i hv tried a lot but still cant get the result – Trainee Mar 17 '17 at 01:04
  • @Trainee - there you go - NOTE: it's written to count weekdays, for weekends, you can either subtract from total, or figure the reconciliation yourself (btw - weekdays is the more complicated, why I supplied that) – John Mar 17 '17 at 16:12
0

i found my own solution, but i forgot to share it. this is how i make it

function myUpdate(dt1, dt2,range){
        var msec1 = dt1;            
        var date1 =  new Date(msec1);
        var msec2 = dt2;
        var date2 =  new Date(msec2);
        var diff = (isFinite(Math.round (date2 - date1) / 86400000) && Math.round (date2 - date1) / 86400000 || 0);
        var wEnd=0;

        if(date1>0 || date2>0){
                for(var i=0; i<=diff; i++){
                    if(date1.getDay() ==6 || date1.getDay()==0){
                        wEnd = wEnd + 1;
                    }
                    date1.setDate(date1.getDate() + 1);
                }
            }
        range.val(Math.round((diff-wEnd)+1));
    }; 

first u should count the different date, then the date1 will be check if it is sunday or saturday. then date1 will be added 1 till the value of date1 is equal to date2. if date1 is/are saturday or sunday, wEnd will gain 1. so u can substract diff with wEnd. hope this can help u guys

Trainee
  • 15
  • 7