以天数计算两个日期之间的差异

时间:2014-08-08 11:06:04

标签: javascript jquery html jquery-ui datepicker

我想要做的是,当用户点击按钮时,我想计算并显示两个日期之间的差异。

function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
    var iWeeks, iDateDiff, iAdjust = 0;

    if (dDate2 < dDate1) return -1; // error code if dates transposed

    var iWeekday1 = dDate1.getDay(); // day of week
    var iWeekday2 = dDate2.getDay();
    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;

    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend

    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

    if (iWeekday1 <= iWeekday2) {
        iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
    } else {
        iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
    }

    iDateDiff -= iAdjust // take into account both days on weekend

    return (iDateDiff + 1); // add 1 because dates are inclusive
}

$("#datepicker, #datepicker1").change(function() {
    alert("getPricing just got called");
    var d1 = $("#datepicker").val();
    var d2 = $("#datepicker1").val();

    var minutes = 1000*60;
    var hours = minutes*60;
    var day = hours*24;

    var startdate1 = getDateFromFormat(d1, "d-m-y");
    var enddate1 = getDateFromFormat(d2, "d-m-y");

    var newstartdate=new Date();
    newstartdate.setFullYear(startdate1.getYear(),startdate1.getMonth(),startdate1.getDay());

    var newenddate=new Date();
    newenddate.setFullYear(enddate1.getYear(),enddate1.getMonth(),enddate1.getDay());

    var days = calcBusinessDays(newstartdate,newenddate);

    if(days>0) {
        $("#leaves_left").val(days);
    } else {
        $("#leaves_left").val(0);
    }
});

$(function() {
    //alert("getPricing just got called");

    $("#datepicker").datepicker({
        beforeShowDay: $.datepicker.noWeekends, 
        dateFormat: 'yy-mm-dd',
        minDate: 0,
        beforeShow: function() {
            $('.datepicker').datepicker('option', 'maxDate', $('#end_date').val());
        }
    });

    $("#datepicker1" ).datepicker({
        beforeShowDay: $.datepicker.noWeekends, 
        dateFormat: 'yy-mm-dd',
        beforeShow: function() {
            $(this).datepicker('option', 'minDate', $('#datepicker').val());
        }
    });

    $("#button").submit(function(){ 
        alert("getPricing just got called");
        var start   = $('#datepicker').datepicker('getDate');
        var end = $('#datepicker1').datepicker('getDate');
        var days   = (end - start)/1000/60/60/24;
        alert(days);
    });
});

screenshot http://oi60.tinypic.com/2a7tn4x.jpg

HTML部分

<form action="#" method="post" > 
                <dl>
                <dt><span> Leave Reason:</span>
                  <textarea name="reason" required></textarea>
                </dt>
                <br>
              <dd><span> Start Date:</span>
                </dd><br>
              <dd><input type="submit" value="Request" id="button" name="submit"></dd></dl></form>    <input type="text" id="datepicker" size="25" name ="date1"></dd>
              <br>              <dd><span> Expiry Date:</span>&nbsp;&nbsp;&nbsp; <input type="text" id="datepicker1" size="25" name ="date2"><br><br>

1 个答案:

答案 0 :(得分:0)

在您的示例中,您已经在计算使用日期选择器更改日期之间的工作日。在这里,我改进了这部分,仍然使用从另一个stackOverflow问题中复制的calcBusinessDays方法:

$("#datepicker, #datepicker1").change(function() {
    //alert("getPricing just got called");
    var d1 = $("#datepicker").datepicker("getDate");
    var d2 = $("#datepicker1").datepicker("getDate");

    // Calculate only if both dates are defined
    if(d1 != undefined && d2 != undefined) {
        var days = calcBusinessDays(d1,d2);
        if(days > 0) {
            $("#leaves_left").val(days);
        } else {
            $("#leaves_left").val(0);
        }
    }
});

您可以外部化此方法的一部分,以便在按钮单击时使用它:

var d1 = $("#datepicker").datepicker("getDate");
var d2 = $("#datepicker1").datepicker("getDate");

// Calculate only if both dates are defined
if(d1 != undefined && d2 != undefined) {
    var days = calcBusinessDays(d1,d2);
    alert(days);
}

顺便说一下,您还应该使用dl dd dt标记来表示您要尝试实施的内容。我不能给你一个与你的结构相对应的小提琴,只是一个基本样本:http://jsfiddle.net/nrobert/6h85yvrg/