Find day difference between two dates (excluding weekend days)

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

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?

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.

This is the code im using right now:

 

回答1:

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

In raw javascript i will use:

Live Demo

 

found here

and to call that function, for example, something like:

   

## EDITED ##

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

Your code will look like:

 


回答2:

This is how I would do it

function getDays(d1, d2) {     var one_day=1000*60*60*24;     var d1_days = parseInt(d1.getTime()/one_day) - 1;     var d2_days = parseInt(d2.getTime()/one_day);     var days = (d2_days - d1_days);     var weeks = (d2_days - d1_days) / 7;     var day1 = d1.getDay();     var day2 = d2.getDay();     if (day1 == 0) {         days--;     } else if (day1 == 6) {         days-=2;     }     if (day2 == 0) {        days-=2;     } else if (day2 == 6) {        days--;     }     days -= parseInt(weeks) * 2;     alert(days); }  getDays(new Date("June 8, 2004"),new Date("February 6, 2010")); 

EDIT
To clarify my comment to @keenebec...
That solution will work for small date differences quite nicely and is easy to understand. But take something as "short" as a 6 year span and you can see a remarkable difference in speed.

http://jsfiddle.net/aSvxv/

I included all 3 answers and the original answer is indeed the fastest, but not by much and the trade off for a few microseconds of execution is somewhat trivial to me in favor of readability.



回答3:

To do this, you should NOT search all days between these dates !

It's not complicated, look some evident assumptions:

  1. All full-week has 7-days.

  2. Which 2 are weekend-days.

  3. And which 5 are business-day.

Evident conclusions:

  1. Look all days is loss of time.

  2. Check what day is weekend to all week is loss of time.


Without tedious explanation.. let me show the code:

function getBusinessDateCount (startDate, endDate) {     var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;     var ifThen = function (a, b, c) {         return a == b ? c : a;     };      elapsed = endDate - startDate;     elapsed /= 86400000;      daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;     daysAfterLastSunday = endDate.getDay();      elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);     elapsed = (elapsed / 7) * 5;     elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);      return Math.ceil(elapsed); }   var date1 = new Date(1999, 12, 31); var date2 = new Date(); // now  print( getBusinessDateCount(date1, date2) ); 

You can test it yourself with any dates.

I just want to notice that this code ONLY consumed 0.43 sec between dates from 2000 to 2015... It is much more fast than some other codes.

Hope it helps...

Nice coding !!



回答4:

Date.prototype.addDays = function(days) {     var date = new Date(this.valueOf())     date.setDate(date.getDate() + days);     return date; }  function getBusinessDatesCount(startDate, endDate) {     var count = 0;     var curDate = startDate;     while (curDate 
 


回答5:

That looks like too much work to me. I'd rather let the computer do the heavy lifting- //

Date.bizdays= function(d1, d2){     var bd= 0, dd, incr=d1.getDate();     while(d1


回答6:

To understand way.,


  1. Actual days = 14
  2. weeks for Actual days = 14/7=2
  3. Weekends per week=2
  4. Total weekends=2*weeks for days

So apply this ,

 $('#EndDate').on('change', function () {             var start = $('#StartDate').datepicker('getDate');             var end = $('#EndDate').datepicker('getDate');             if (start 

Thank you !



回答7:

I get it work with this code. Note that the function is from date.js and businessday js (thanks to Garis Suero). Start Date 11-08-2010 End Date 16-08-2010 will result 4 days of leave.

 


回答8:

What I did

function calcbusinessdays() {     for(var c=0,e=0,d=new Date($("#startdate").val()),a=(new Date($("#enddate").val())-d)/864E5;0

c is weekdays, e is weekends



回答9:

function addDays(date, days) {     var result = new Date(date);     result.setDate(result.getDate() + days);     return result; }  var currentDate;                             selectFlixbleDates = [];                             var monToSatDateFilter=[];                             currentDate=new Date(date);                             while(currentDate){                                 console.log("currentDate"+currentDate);                                 if(new Date(currentDate).getDay()!=0){                                     selectFlixbleDates.push(currentDate)                                 }                                 if(selectFlixbleDates.length==$scope.numberOfDatePick)                                 {                                      break;                                 }                                 currentDate=addDays(currentDate,1);                              }                             for (var i = 0; i 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!