嗨,我正在使用jquery-uidatepicker选择date和date.js来查找两个日期之间的差异。
现在的问题是我想从计算中排除周末(周六和周日)。我该怎么办?
例如,用户选择开始日期(13/8/2010)和结束日期(16/8/2010)。由于14/8/2010和15/8/2010是工作日,而不是总共4天,所以我希望只有2天。
这是即时消息正在使用的代码:
<script type="text/javascript"> $("#startdate, #enddate").change(function() { var d1 = $("#startdate").val(); var d2 = $("#enddate").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 days = 1 + Math.round((enddate1 - startdate1)/day); if(days>0) { $("#noofdays").val(days);} else { $("#noofdays").val(0);} }); </script>
也许其他人可以帮助您将此功能转换为JQuery的框架…
我在这里找到了这个功能。
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) { //Equal to makes it reduce 5 days 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 } var date1 = new Date("August 11, 2010 11:13:00"); var date2 = new Date("August 16, 2010 11:13:00"); alert(calcBusinessDays(date1, date2));
如果您想将其与该格式一起使用,请执行以下操作:
您的代码如下所示:
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) { //Equal to makes it reduce 5 days 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 } $("#startdate, #enddate").change(function() { var d1 = $("#startdate").val(); var d2 = $("#enddate").val(); var minutes = 1000 * 60; var hours = minutes * 60; var day = hours * 24; var startdate1 = new Date(d1); var enddate1 = new Date(d2); 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) { $("#noofdays").val(days); } else { $("#noofdays").val(0); } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>Start Date <input type="date" id="startdate" value="2019-03-03"/> </label> <label>End Date <input type="date" id="enddate" value="2019-03-06"/> </label> <label>N. of days <output id="noofdays"/> </label>