Exclude weekends based on javascript

I have two sets of codes that work. Need help, combining them into one.

This code gives me the difference between two dates. works great:

function test(){ var date1 = new Date(txtbox_1.value); var date2 = new Date(txtbox_2.value); var diff = (date2 - date1)/1000; var diff = Math.abs(Math.floor(diff)); var days = Math.floor(diff/(24*60*60)); var leftSec = diff - days * 24*60*60; var hrs = Math.floor(leftSec/(60*60)); var leftSec = leftSec - hrs * 60*60; var min = Math.floor(leftSec/(60)); var leftSec = leftSec - min * 60; txtbox_3.value = days + "." + hrs; } 

source for the above code

The @cyberfly code below has an answer to the exception of sitting and the sun, which is what I need. source . However, its in jquery and above, the code is in JS. Therefore, help is needed, combining, because I lacked this knowledge: (

 <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, "dmy"); var enddate1 = getDateFromFormat(d2, "dmy"); var days = calcBusinessDays(new Date(startdate1),new Date(enddate1)); if(days>0) { $("#noofdays").val(days);} else { $("#noofdays").val(0);} }); </script> 

EDIT I tried to combine codes. here is my example. getting the expected error of the object.

 function test(){ var date1 = new Date(startdate.value); var date2 = new Date(enddate.value); var diff = (date2 - date1)/1000; var diff = Math.abs(Math.floor(diff)); var days = Math.floor(diff/(24*60*60)); var leftSec = diff - days * 24*60*60; var hrs = Math.floor(leftSec/(60*60)); var leftSec = leftSec - hrs * 60*60; var min = Math.floor(leftSec/(60)); var leftSec = leftSec - min * 60; var startdate1 = getDateFromFormat(startdate, "dd/mm/yyyy hh:mm"); var enddate1 = getDateFromFormat(enddate, "dd/mm/yyyy hh:mm"); days = calcBusinessDays(new Date(startdate1),new Date(enddate1)); noofdays.value = days + "." + hrs; } start: <input type="text" id="startdate" name="startdate" value="02/03/2015 00:00"> end: <input type="text" id="enddate" name="enddate" value="02/03/2015 00:01"> <input type="text" id="noofdays" name="noofdays" value=""> 

+6
source share
3 answers

When determining the number of days between two dates, there are many decisions about what a day is. For example, the period from February 1 to February 2 is usually one day, so from February 1 to February 1, they are zero.

When adding the complexity of counting only business days, everything becomes much more complicated. For instance. Monday February 2, 2015 to Friday February 6 - 4 past days (Monday to Tuesday - 1, Monday to Wednesday - 2, etc.), However, the expression "Monday to Friday" is usually considered as 5 working days and duration Mon 2 February to Saturday 7 February should also be 4 business days, but from Sunday to Saturday should be 5.

So here is my algorithm:

  • Get the total number of whole days between two dates
  • Divide by 7 to get the number of whole weeks.
  • Multiply the number of weeks by two to get the number of days off.
  • Subtract the number of days off from the whole to get the working days.
  • If the number of total days is not equal to the even number of weeks, add numbe weeks * 7 to the start date to get the temp date
  • So far, the temp date is less than the end date:
    • if the time is not Saturday or Sunday, add one business day
    • add one to temp date
  • What is it.

The step-by-step part at the end can probably be replaced by some other algorithm, but there will never be a cycle of more than 6 days, so this is a simple and reasonably effective solution to the problem of unequal weeks.

Some consequences of the above:

  • Monday - Friday - 4 business days.
  • Any day on the same day in another week is an even number of weeks and, therefore, an even choice of 5, for example. Monday from February 2 to Monday February 9 and Sunday from February 1 to Sunday February 8 - 5 business days.
  • Friday from February 6 to Sunday February 7 are zero business days.
  • Friday from February 6 to Monday February 9 - one business day.
  • Sunday February 8th to Sunday February 15th, Saturday February 14th and Friday February 13th all 5 working days.

Here is the code:

 // Expects start date to be before end date // start and end are Date objects function dateDifference(start, end) { // Copy date objects so don't modify originals var s = new Date(+start); var e = new Date(+end); // Set time to midday to avoid dalight saving and browser quirks s.setHours(12,0,0,0); e.setHours(12,0,0,0); // Get the difference in whole days var totalDays = Math.round((e - s) / 8.64e7); // Get the difference in whole weeks var wholeWeeks = totalDays / 7 | 0; // Estimate business days as number of whole weeks * 5 var days = wholeWeeks * 5; // If not even number of weeks, calc remaining weekend days if (totalDays % 7) { s.setDate(s.getDate() + wholeWeeks * 7); while (s < e) { s.setDate(s.getDate() + 1); // If day isn't a Sunday or Saturday, add to business days if (s.getDay() != 0 && s.getDay() != 6) { ++days; } } } return days; } 

I don’t know how it compares with the jfriend00 answer or the code you referenced, if you want the period to be included, just add it if the start or end date is a business day.

+8
source

Here's a simple function to calculate the number of working days between two date objects. As planned, it does not take into account the start day, but calculates the end of the day, so if you give it a date on Tuesday of one week and on Tuesday of the next week, it will return in 5 working days. This does not take into account the holidays, but it works correctly when the daytime changes.

 function calcBusinessDays(start, end) { // This makes no effort to account for holidays // Counts end day, does not count start day // make copies we can normalize without changing passed in objects var start = new Date(start); var end = new Date(end); // initial total var totalBusinessDays = 0; // normalize both start and end to beginning of the day start.setHours(0,0,0,0); end.setHours(0,0,0,0); var current = new Date(start); current.setDate(current.getDate() + 1); var day; // loop through each day, checking while (current <= end) { day = current.getDay(); if (day >= 1 && day <= 5) { ++totalBusinessDays; } current.setDate(current.getDate() + 1); } return totalBusinessDays; } 

And, jQuery + jQueryUI code to demonstrate:

 // make both input fields into date pickers $("#startDate, #endDate").datepicker(); // process click to calculate the difference between the two days $("#calc").click(function(e) { var diff = calcBusinessDays( $("#startDate").datepicker("getDate"), $("#endDate").datepicker("getDate") ); $("#diff").html(diff); }); 

And here is a simple demo created using date picker in jQueryUI: http://jsfiddle.net/jfriend00/z1txs10d/

+2
source

@RobG provided an excellent algorithm for separating weekdays from weekends. I think the only problem is that the starting days are weekends, Saturdays or Sundays, then the number of working days / days off will be less.

The code is fixed below.

 function dateDifference(start, end) { // Copy date objects so don't modify originals var s = new Date(start); var e = new Date(end); var addOneMoreDay = 0; if( s.getDay() == 0 || s.getDay() == 6 ) { addOneMoreDay = 1; } // Set time to midday to avoid dalight saving and browser quirks s.setHours(12,0,0,0); e.setHours(12,0,0,0); // Get the difference in whole days var totalDays = Math.round((e - s) / 8.64e7); // Get the difference in whole weeks var wholeWeeks = totalDays / 7 | 0; // Estimate business days as number of whole weeks * 5 var days = wholeWeeks * 5; // If not even number of weeks, calc remaining weekend days if (totalDays % 7) { s.setDate(s.getDate() + wholeWeeks * 7); while (s < e) { s.setDate(s.getDate() + 1); // If day isn't a Sunday or Saturday, add to business days if (s.getDay() != 0 && s.getDay() != 6) { ++days; } //s.setDate(s.getDate() + 1); } } var weekEndDays = totalDays - days + addOneMoreDay; return weekEndDays; } 

JSFiddle Link https://jsfiddle.net/ykxj4k09/2/

+1
source

Source: https://habr.com/ru/post/981890/


All Articles