问题
Was searching for a solution to display the current workday over the total workdays in the current month. Workdays in this case are defined as Mon, Tues, Wed, Thurs, & Fri.
Closest solution I found was this: How to find business days in current month with Javascript?
Using this post's suggested answer with a slight edit, how can I edit this to display the current workday and total workdays in the month?
Here is my CodePen attempt.
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);
}
回答1:
Here's my solution; should be pretty self-explanatory:
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}.`
The loop goes through all days of the current month and counts all weekdays and past weekdays.
Edit: added (incomplete) holiday array and check
回答2:
Here's my crack at it!
The first bit creates an array with every day of the month in it:
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);
and the second part counts every workday in it:
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++
}
回答3:
This returns all business days if you input the first day of the month '01/07/18'. It excludes federal holidays through 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)
]
* */
回答4:
I'll try to make a solution by solving this step by step and keeping each step as simple as posible.
// 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
Hope this helps to make it easier to reason about!
来源:https://stackoverflow.com/questions/50807164/how-to-display-total-workdays-in-month-and-current-workday-javascript