A Simple Example of Calculating a Workday for a Two Day Period in js

  • 2021-07-09 07:02:33
  • OfStack

Examples are as follows:


// Start date, /pattern/ Is the delimiter of a regular expression, pattern Is the content to match, and is only used for the 1 The matching of symbols, g Flag for global match 
var beginDate = new Date("2013-01-01".replace(/-/g, "/"));
// End date 
var endDate = new Date("2013-01-31".replace(/-/g, "/"));
// Date difference , That is, including weeks 6 Day, working hours in days, 86400000=1000*60*60*24.
var workDayVal = (endDate - beginDate)/86400000 + 1;
// Remainder of working hours 
var remainder = workDayVal % 7 ; 
// Divisor rounded down by work 
var divisor = Math.floor(workDayVal / 7);
var weekendDay = 2 * divisor;

// The week of the start date, and the value of the week is ( 1,2,3,4,5,6,0 ) 
var nextDay = beginDate.getDay();
// Starting from the week of the start date   Traversal remainder Days 
for(var tempDay = remainder; tempDay>=1; tempDay--) {
  // No. 1 1 There is no need to add it 1
  if(tempDay == remainder) {
    nextDay = nextDay + 0;
  } else if(tempDay != remainder) {
    nextDay = nextDay + 1;
  }
  // On Sunday, it was changed to 0
  if(nextDay == 7) {
    nextDay = 0;
  }

  // Week 6 Day 
  if(nextDay == 0 || nextDay == 6) {
    weekendDay = weekendDay + 1;
  }
}
// Actual working hours (days)  =  Date difference between start and end  -  Week 6 Number of days. 
workDayVal = workDayVal - weekendDay;

Related articles: