用JS计算两个日期之间有多少个休息日

如果把两头的日期都算在内,只计算周六周日的话. 可以这样, 或许还有简单一些的计算方法,不管那么多了,先弄一个能工作的出来再说.

<script type="text/javascript">

 function weekendBetween(dtStart, dtEnd) {   
     if (typeof dtEnd == 'string' ) 
         dtEnd = StringToDate(dtEnd); 
     if (typeof dtStart == 'string' )  
         dtStart = StringToDate(dtStart); 
 
     var days =  parseInt((dtEnd - dtStart) / 86400000); 
     var adds = 0;
     var m = (days+ dtStart.getDay() +1) % 7;
     if(m > 0)
        adds = m;
     if(m >2)
        adds = 2;
    
     var redu = 0
     if(dtStart.getDay() > 0)
        redu = 2;
     if(dtStart.getDay() ==1)
        redu =1;
       
     var subtotal = parseInt((days + dtStart.getDay() +1)/7)*2;
    return subtotal - redu + adds;

 }
  function StringToDate(DateStr) 
 {  
     var converted = Date.parse(DateStr); 
     var myDate = new Date(converted); 
    if (isNaN(myDate)) 
     {  
         var arys= DateStr.split('-'); 
         myDate = new Date(arys[0],arys[1],arys[2]); 
     } 
     return myDate; 
 }


alert("2-1~~~~2-18");
alert(weekendBetween(new Date(2008,2,1), new Date(2008,2,18)));

alert("2-1~~~~2-15,  字符型参数");
alert(weekendBetween("2008-2-1","2008-2-15"));

alert("2-1~~~~2-16,  字符型参数");
alert(weekendBetween("2008-2-1","2008-2-16"));

alert("2-1~~~~2-17,  字符型参数");
alert(weekendBetween("2008-2-1","2008-2-17"));

alert("2-3~~~~2-16,  字符型参数");
alert(weekendBetween("2008-2-3","2008-2-16"));

</script>

posted on 2008-02-14 23:53  沙加  阅读(1783)  评论(0编辑  收藏  举报

导航