Возвращает все рабочие дни, если вы вводите первый день месяца '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)
]
* */