Header Ads Widget

Responsive Advertisement

Days Between Two Dates With & Without Sunday in JavaScript


Description:

                   That script is useful for find number of days between two dates with and without specific day (Sunday). That code take two date parameters then calculate total days between given dates.

<script>
//Given Dates

var sDate='4/28/2018';//mm/dd/yyyy

var eDate='5/1/2018';//mm/dd/yyyy

var startDate = new Date(sDate);

var endDate = new Date(eDate);



//get total sundays

var totalSundays = 0;



for (var i = startDate; i <= endDate; ){

    if (i.getDay() == 0){

        totalSundays++;

    }

    i.setTime(i.getTime() + 1000*60*60*24);

}



prompt("Total Sundays",totalSundays);

//******End******



//count days between two dates

function parseDate(str) {

    var mdy = str.split('/');

    return new Date(mdy[2], mdy[0]-1, mdy[1]);

}



function datediff(first, second) {

    // Take the difference between the dates and divide by milliseconds per day.

    // Round to nearest whole number to deal with DST.

    return Math.round((second-first)/(1000*60*60*24));

}

prompt("Total Days",datediff(parseDate(sDate), parseDate(eDate)));

prompt("Total Days Without Sunday",datediff(parseDate(sDate), parseDate(eDate))-totalSundays);


</script>

Post a Comment

0 Comments