我想从特定年份的选择月份计算工作日的数量[表示不包括星期六和星期日]:
例如
如果2017年是选定年份,而2月份是选定月份, 然后输出应该是:
{
week1 : 3 days[working days]
week2 : 5 days[working days]
week3 : 5 days[working days]
week4 : 5 days[working days]
week5 : 2 days[working days]
}
2017年2月的总工作日:20个工作日。
我想在Angular 2,typescript中实现这一点。
P.S:我是Angular2和打字稿的新手,你可以说是初学者。
任何帮助感谢, 提前致谢
答案 0 :(得分:0)
好旧的javascript:
"use strict";
var daysExclude = [0, 6],
days = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
// val is a comma separated month and year
// e.g.: 7,2017 or 04, 2015
// it can be just a number too (5, 9, etc.) in which case code uses current year
function getWorkingDays(val) {
if(!val) { return false; }
var wCount, out = {start: null, end: null, days: []};
val = val.split(',');
// subtract 1 as month is 0 based
val[0] = parseInt(val[0]) - 1;
if(val[0] > 11) { val[0] = val[0]%12; }
// if year not provided, use current year
if(typeof val[1] == 'undefined') {
val[1] = new Date();
val[1] = val[1].getFullYear();
}
// get first day of the current and next month
out.start = new Date(val[1], val[0]);
out.end = new Date(val[1], val[0] + 1);
// save only the day number (0 - Sunday, ... 6 - Saturday)
out.start = out.start.getDay();
out.end = out.end.getDay();
// increase out.end by a 28 or 35
if(out.end < out.start) { out.end += 7; }
out.end += 28;
// calculate working days
var i = out.start;
while(i < out.end) {
wCount = Math.floor(i/7);
if(typeof out.days[wCount] == 'undefined') { out.days[wCount] = 0; }
if(daysExclude.indexOf(i%7) == -1) { ++out.days[wCount]; }
++i;
}
// calculate output
out.start = days[out.start] + 'day';
out.end = days[(out.end-1)%7] + 'day';
console.log(out);
return out;
}