Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

datetime - exclude weekends in javascript date calculation

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

This code gets me the difference between two dates. works perfectly:

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 code below by @cyberfly appears to have the answer of excluding sat and sun which is what i needed. source. However, its in jquery and the above code is in JS. Therefore, needed help combining as i lacked that 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, "d-m-y");
        var enddate1 = getDateFromFormat(d2, "d-m-y");

        var days = calcBusinessDays(new Date(startdate1),new Date(enddate1));             

if(days>0)
{ $("#noofdays").val(days);}
else
{ $("#noofdays").val(0);}


});

</script>

EDIT Made an attempt at combining the codes. here is my sample. getting object expected error.

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When determining the number of days between two dates, there are lots of decisions to be made about what is a day. For example, the period 1 Feb to 2 Feb is generally one day, so 1 Feb to 1 Feb is zero days.

When adding the complexity of counting only business days, things get a lot tougher. E.g. Monday 2 Feb 2015 to Friday 6 February is 4 elapsed days (Monday to Tuesday is 1, Monday to Wednesday is 2, etc.), however the expression "Monday to Friday" is generally viewed as 5 business days and the duration Mon 2 Feb to Sat 7 Feb should also be 4 business days, but Sunday to Saturday should be 5.

So here's my algorithm:

  1. Get the total number of whole days between the two dates
  2. Divide by 7 to get the number of whole weeks
  3. Multiply the number of weeks by two to get the number of weekend days
  4. Subtract the number of weekend days from the whole to get business days
  5. If the number of total days is not an even number of weeks, add the numbe of weeks * 7 to the start date to get a temp date
  6. While the temp date is less than the end date:
  • if the temp date is not a Saturday or Sunday, add one the business days
  • add one to the temp date
  1. That's it.

The stepping part at the end can probably be replaced by some other algorithm, but it will never loop for more than 6 days so it's a simple and reasonably efficient solution to the issue of uneven weeks.

Some consequences of the above:

  1. Monday to Friday is 4 business days
  2. Any day to the same day in a different week is an even number of weeks and therefore an even mutiple of 5, e.g. Monday 2 Feb to Monday 9 Feb and Sunday 1 Feb to Sunday 8 Feb are 5 business days
  3. Friday 6 Feb to Sunday 7 Feb is zero business days
  4. Friday 6 Feb to Monday 9 Feb is one business day
  5. Sunday 8 Feb to: Sunday 15 Feb, Sat 14 Feb and Fri 13 Feb are all 5 business days

Here's 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 to jfriend00's answer or the code you referenced, if you want the period to be inclusive, just add one if the start or end date are a business day.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...