如何在月份和当前工作日javascript中显示总工作日

时间:2018-06-11 23:00:44

标签: javascript html datetime

正在搜索解决方案,以显示当月工作日的当前工作日。在这种情况下,工作日定义为周一周二周三周四,&的周五

我找到的最近的解决方案是:How to find business days in current month with Javascript?

使用此帖子的建议答案进行轻微编辑,如何编辑此答案以显示当月的当前工作日和工作日总数?

这是我的CodePen尝试。

HTML:

<h3>Get workdays in current month with JavaScript</h3>
<p>Current Day: <span id="currentDay"></span></p>
<p>Total Days: <span id="totalDays"></span></p>

JS:

function isWeekday(year, month, day) {
    var day = new Date(year, month, day).getDay();
    return day !=0 && day !=6;
}
function getWeekdaysInMonth(month, year) {
    var days = daysInMonth(month, year);
    var weekdays = 0;
    for(var i=0; i< days; i++) {
        if (isWeekday(year, month, i+1)) weekdays++;
    }
    $('#totalDays').html(weekdays);
}

4 个答案:

答案 0 :(得分:1)

这是我对它的抨击!

第一位创建一个包含该月每一天的数组:

var d = new Date();
d = new Date(d.getFullYear(), d.getMonth(), 1);
var month;
function setMonth() {
  month = [];
  for (var i = 1; i < new Date(d.getFullYear(), d.getMonth(), 0).getDate()+1; i++) {
    var _d = new Date(d);
    month[i-1] = new Date(_d.setDate(_d.getDate() - _d.getDay()+i));
  }
}

setMonth();

console.log(month);

,第二部分计算其中的每个工作日:

var totalWorkdays = 0;
for (var i = 0; i < month.length; i++) {
    let day = month[i].getDay();
    if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5) totalWorkdays++
}

答案 1 :(得分:1)

这是我的解决方案;应该是不言自明的:

&#13;
&#13;
const holidays = [
  [7, 4], // 4th of July
  [10, 31] // Halloween
];

var d = new Date();
var currentDay = d.getDate();
var year = d.getYear() + 1900;
var month = d.getMonth();
var total = 0;
var done = 0;
for (var day = 1; day <= 31; day++) {
  var t = new Date(year, month, day);
  if (t.getMonth() > month) break; // month has less than 31 days
  if (t.getDay() == 0 || t.getDay() == 6) continue; // no weekday
  if (holidays.some(h => h[0] - 1 === month && h[1] === day)) continue; // holiday
  total++; // increase total
  if (t.getDate() <= currentDay) done++; // increase past days
}
document.body.innerHTML = `Today is weekday ${done} of ${total}.`
&#13;
&#13;
&#13;

循环遍历当月的所有日子,并计算所有工作日和过去的工作日。

编辑:添加(不完整)假日数组并检查

答案 2 :(得分:0)

如果您输入'01 / 07/18'的第一天,则会返回所有工作日。它不包括到2020年的联邦假期。

function day_of_week (date) {
  let weekday = ['Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday']

  return weekday[date.getDay()]

}

function formatDate (date) {
  let d = new Date(date)
  let month = '' + (d.getMonth() + 1)
  let day = '' + d.getDate()
  let year = d.getFullYear()

  if (month.length < 2) month = '0' + month
  if (day.length < 2) day = '0' + day

  return [year, month, day].join('-')
}

function calculate_business_days (inputDate, holidays) {

  Date.prototype.daysPerMonth = function () {
    let d = new Date(this.getFullYear(), this.getMonth() + 1, 0)
    return d.getDate()
  }

  let date = new Date(inputDate)
  let transitionDays = date.daysPerMonth()
  let businessDays = []

  for (let i = 1; i < transitionDays + 1; i++) {

    let nextDay = new Date(inputDate)
    nextDay.setDate(nextDay.getDate() + i)
    let day = day_of_week(nextDay)
    if (day !== 'Saturday') { // exclude Saturday
      if (day !== 'Sunday') { // exclude Sunday
        if (holidays.indexOf(formatDate(nextDay)) === -1) { // exclude holidays through 2020
          businessDays.push(nextDay)
        }
      }
    }

  }

  return businessDays

}

let start_of_month = '06/01/2018'
let business_days_june = calculate_business_days(start_of_month, holidays_through_2020())
console.log(business_days_june)


/** output in console.log()
 * 
 [
   Mon Jun 04 2018 00:00:00 GMT-0600 (MDT),
   Tue Jun 05 2018 00:00:00 GMT-0600 (MDT),
   Wed Jun 06 2018 00:00:00 GMT-0600 (MDT),
   Thu Jun 07 2018 00:00:00 GMT-0600 (MDT),
   Fri Jun 08 2018 00:00:00 GMT-0600 (MDT),
   Mon Jun 11 2018 00:00:00 GMT-0600 (MDT),
   Tue Jun 12 2018 00:00:00 GMT-0600 (MDT),
   Wed Jun 13 2018 00:00:00 GMT-0600 (MDT),
   Thu Jun 14 2018 00:00:00 GMT-0600 (MDT),
   Fri Jun 15 2018 00:00:00 GMT-0600 (MDT),
   Mon Jun 18 2018 00:00:00 GMT-0600 (MDT),
   Tue Jun 19 2018 00:00:00 GMT-0600 (MDT),
   Wed Jun 20 2018 00:00:00 GMT-0600 (MDT),
   Thu Jun 21 2018 00:00:00 GMT-0600 (MDT),
   Fri Jun 22 2018 00:00:00 GMT-0600 (MDT),
   Mon Jun 25 2018 00:00:00 GMT-0600 (MDT),
   Tue Jun 26 2018 00:00:00 GMT-0600 (MDT),
   Wed Jun 27 2018 00:00:00 GMT-0600 (MDT),
   Thu Jun 28 2018 00:00:00 GMT-0600 (MDT),
   Fri Jun 29 2018 00:00:00 GMT-0600 (MDT)
 ]
 * */

答案 3 :(得分:0)

我会尝试通过逐步解决这个问题并保持每个步骤尽可能简单来解决问题。

// Returns true if date is week day
function isWeekday(date) {
  const day = date.getDay();
  return day !=0 && day !=6;
}

// Get dates of all work days in one month
function getWeekdaysInMonth(month, year) {
  return getDaysInMonth(month, year).filter(isWeekday)
}

// Get dates of all days in one month 
function getDaysInMonth(month, year) {
  const firstDay = new Date(year, month, 1);
  const lastDay = new Date(year, month + 1, 0);
  
  return getDaysInRange(firstDay, lastDay);
}

// Get dates between 2 dates
function getDaysInRange(start, end) {
  const r = [];
  for(let dt = start; dt <= end; dt.setDate(dt.getDate() + 1)) {
      r.push(new Date(dt));
  }
  return r;
}

console.log(getWeekdaysInMonth(0, 2018)); // Get workdays for JAN 2018

希望这有助于更容易推理!