NodeJS :计算两个日期之间每个月的工作数

2ul0zpep  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(166)

我正在尝试开发一个代码来生成两个选定日期之间的每月工作日数:
例如:开始日期为2022年10月20日,结束日期为2023年2月14日。
我可以生成两个日期之间的净工作日,但不能为两个日期之间的每个月生成净工作日。
我希望代码提供如下输出:10月22日的净工作日为8,11月22日为21,1月23日为22,2月22日为10。

pb3skfrl

pb3skfrl1#

var startDate = new Date('20/10/2022');
var endDate = new Date('14/02/2023');
var numOfDates = getBusinessDatesCount(startDate,endDate);

function getBusinessDatesCount(startDate, endDate) {
 let count = 0;
const curDate = new Date(startDate.getTime());
while (curDate <= endDate) {
    const dayOfWeek = curDate.getDay();
    if(dayOfWeek !== 0 && dayOfWeek !== 6) count++;
    curDate.setDate(curDate.getDate() + 1);
 }
 return count;
}

就像上面的例子,你可以计算两个日期之间的工作日数。

相关问题