admin管理员组

文章数量:1336633

I have two datepicker from which I can calculate the number of day without counting Saturday and Sunday. But I want to do this for Friday and Saturday. I have tried some different ways but failed. It was easy for me to handle number of day excluding Saturday and Sunday but not for Friday and Saturday Following is my javascript code -

$(function() {
    $( "#startDate" ).datepicker({
        onSelect: calculateDays});
    });

    $(function() {
        $( "#endDate" ).datepicker({
            onSelect: calculateDays});
        });

        function calculateDays(startDate, endDate) {
            var form = this.form

            var startDate = document.getElementById("startDate").value;
            var startDate = new Date(startDate);

            var endDate = document.getElementById("endDate").value;
            var endDate = new Date(endDate);

            startDate.setHours(0,0,0,1); // Start just after midnight
            endDate.setHours(23,59,59,999); // End just before midnight

            var oneDay = 24*60*60*1000;
            var diff = endDate - startDate; // Milliseconds between datetime objects
            var days = Math.ceil(diff / oneDay);

            var weeks = Math.floor(days / 7);
            var days = days - (weeks * 2);

            // Handle special cases
            var startDay = startDate.getDay();
            var endDay = endDate.getDay();

            // Remove weekend not previously removed.
            if (startDay - endDay > 1)
                days = days - 2;

            // Remove start day if span starts on Sunday but ends before Saturday
            if (startDay == 0 && endDay != 6)
                days = days - 1

            // Remove end day if span ends on Saturday but starts after Sunday
            if (endDay == 6 && startDay != 0)
                days = days - 1

            if (days)
                document.getElementById("result").innerHTML=days;
        }

And my HTML is:

<form id="dateDifference" style="width:200px;">
    <input name="startDate" id="startDate" value="" >
    <input name="endDate" id="endDate" value="" >
</form>
<span id="result"></span>

I have two datepicker from which I can calculate the number of day without counting Saturday and Sunday. But I want to do this for Friday and Saturday. I have tried some different ways but failed. It was easy for me to handle number of day excluding Saturday and Sunday but not for Friday and Saturday Following is my javascript code -

$(function() {
    $( "#startDate" ).datepicker({
        onSelect: calculateDays});
    });

    $(function() {
        $( "#endDate" ).datepicker({
            onSelect: calculateDays});
        });

        function calculateDays(startDate, endDate) {
            var form = this.form

            var startDate = document.getElementById("startDate").value;
            var startDate = new Date(startDate);

            var endDate = document.getElementById("endDate").value;
            var endDate = new Date(endDate);

            startDate.setHours(0,0,0,1); // Start just after midnight
            endDate.setHours(23,59,59,999); // End just before midnight

            var oneDay = 24*60*60*1000;
            var diff = endDate - startDate; // Milliseconds between datetime objects
            var days = Math.ceil(diff / oneDay);

            var weeks = Math.floor(days / 7);
            var days = days - (weeks * 2);

            // Handle special cases
            var startDay = startDate.getDay();
            var endDay = endDate.getDay();

            // Remove weekend not previously removed.
            if (startDay - endDay > 1)
                days = days - 2;

            // Remove start day if span starts on Sunday but ends before Saturday
            if (startDay == 0 && endDay != 6)
                days = days - 1

            // Remove end day if span ends on Saturday but starts after Sunday
            if (endDay == 6 && startDay != 0)
                days = days - 1

            if (days)
                document.getElementById("result").innerHTML=days;
        }

And my HTML is:

<form id="dateDifference" style="width:200px;">
    <input name="startDate" id="startDate" value="" >
    <input name="endDate" id="endDate" value="" >
</form>
<span id="result"></span>
Share Improve this question edited Feb 24, 2014 at 20:29 HJ05 1,3682 gold badges11 silver badges23 bronze badges asked Feb 24, 2014 at 19:54 Md Riad HossainMd Riad Hossain 2915 silver badges21 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3
// Count days from d0 to d1 inclusive, excluding Fridays and Saturdays
function calculateDays( d0, d1 )
{
    var ndays = 1 + Math.round((d1.getTime()-d0.getTime())/(24*3600*1000));
    var nsaturdays = Math.floor((ndays + d0.getDay()) / 7);
    return ndays - 2*nsaturdays + (d0.getDay()==6) - (d1.getDay()==5);
}

where d0 and d1 are the start and end Date objects (with same hour of day values).

one more approach.

for(var i = startDate ; i <= endDate ;i.setDate(i.getDate() + 1)){
            if( i.getDay() != 6 && i.getDay() != 0)
            {
                days = days + 1;
                }
            }

It will give you no.of days except saturday and Sunday

javascript Date method, getDay() will return 0 for Sunday, 1 for Monday, etc.

You should be able to use the date objects and check for the current day of the week and exclude them from the count for 5 (Friday) and 6 (Saturday).

http://www.w3schools./jsref/jsref_getday.asp

If you want to calculate number of working days in the year:

const monthWorkDays = (year, month) => 
new Array(32 - new Date(year, month, 32).getDate())
    .fill(1)
    .filter(
        (id, index) =>
            [0, 6].indexOf(
                new Date(year, month, index + 1).getDay()) === -1
            ).length;

const yearWorkDays = (year) =>
new Array(12)
    .fill(1)
    .reduce((pv, cv, index) => pv + monthWorkDays(year, index), 0);

yearWorkDays(2017)

本文标签: Getting number of working days with javascript and jQuery UI datepickerStack Overflow