查找两个日期之间的差异(周末除外) [英] Find day difference between two dates (excluding weekend days)

查看:132
本文介绍了查找两个日期之间的差异(周末除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用jquery-ui datepicker选择日期并 date.js 找出2个日期之间的差异。

Hi i am using jquery-ui datepicker to select date and date.js to find difference between 2 dates.

现在问题是我想从计算中排除周末天数(星期六和星期日)。我该怎么做?

Right now the problem is I want to exclude weekend days from calculation (saturday and sunday). How should i do that?

例如,用户选择开始日期(13/8/2010)和结束日期(16/8/2010)。由于2010年8月14日和15/8/2010是在工作日,而不是总共4天,我希望它只有2天。

For example the user select start date (13/8/2010) and end date (16/8/2010). Since 14/8/2010 and 15/8/2010 is in week days, instead of 4 days total, i want it to be only 2 days.

这是代码即时使用:

<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的框架...

Maybe someone else can help you converting this function into JQuery's framework...

在原始javascript中,我将使用:

In raw javascript i will use:

现场演示

<script>
  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
  }
</script>

发现这里

并调用该函数,例如:

  <script>
    alert(calcBusinessDays(new Date("August 11, 2010 11:13:00"),new Date("August 16, 2010 11:13:00")));
  </script>



## EDITED ##



如果你只想使用它的格式:

## EDITED ##

If you want to use it with your that format just:

你的代码如下:

<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 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>

这篇关于查找两个日期之间的差异(周末除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆