我想知道是否可以按月和年獲得作業日(周一至周五)。輸入是月 年,輸出日期串列。例如:我給出第 1 個月和 2020 年。我想得到:周一 01-01、周二 02-01、周三 03-01、周四 04-01、周五 05-01、周一 08-01 等。
uj5u.com熱心網友回復:
是的,這是可能的。獲取您本月的第一天。然后遍歷所有天以找到作業日。
function getWeekDates(_year, _month) {
let firstDay = new Date(_year, _month);
const month = firstDay.getMonth();
const weekDays = [];
for (let i = 1; i < 32; i ) {
const date = new Date(_year, month, i);
// the ith day of the month is in the next month, so we stop looping
if (date.getMonth() !== month) {
break;
}
const day = date.getDay();
// push to week days if it's not a Sunday or a Saturday
if (day > 0 && day < 6) {
weekDays.push(formatDate(date));
}
}
return weekDays;
}
function formatDate(date) {
// replace this by your favourite date formatter
const days = ["Sun", "Mon", "Tues", "Wed", "Thrus", "Fri", "Sat"];
return `${days[date.getDay()]} ${date.getDate()}-${date.getMonth() 1}`;
}
// Get for September 2022 (Note months start with January being index 0)
getWeekDates(2022, 8);
uj5u.com熱心網友回復:
您可以簡單地嘗試創建一個陣列,該陣列將首先包含每個月第一天的日期物件。它應該是這樣的:const MyDates= ["1ST DATE OBJECT", "2ND DATE OBJECT", ..., "upto Dec"];
然后,您可以嘗試遍歷每個日期物件并使用 getDay() 方法來獲取該特定日期的作業日。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510307.html
