Javascript 如何显示月份的总工作日和当前工作日
正在搜索一个解决方案,以显示当前月总工作日中的当前工作日。这种情况下的工作日定义为周一、周二、周三、周四和周五 我找到的最接近的解决方案是: 用这篇文章的建议答案稍加编辑,我如何编辑它来显示当前的工作日和当月的总工作日 这是我的尝试 HTML:Javascript 如何显示月份的总工作日和当前工作日,javascript,html,datetime,Javascript,Html,Datetime,正在搜索一个解决方案,以显示当前月总工作日中的当前工作日。这种情况下的工作日定义为周一、周二、周三、周四和周五 我找到的最接近的解决方案是: 用这篇文章的建议答案稍加编辑,我如何编辑它来显示当前的工作日和当月的总工作日 这是我的尝试 HTML: <h3>Get workdays in current month with JavaScript</h3> <p>Current Day: <span id="currentDay"></span&
<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>
使用JavaScript获取当月的工作日
当日:
总天数:
JS:
函数为工作日(年、月、日){
var day=新日期(年、月、日).getDay();
返回日期!=0&&day!=6;
}
函数getWeekdaysInMonth(月,年){
var天数=日月(月、年);
var工作日=0;
对于(变量i=0;i<天;i++){
if(isWeekday(年、月、i+1))工作日++;
}
$('#totalDays').html(工作日);
}
这是我的破绽
第一位创建一个数组,其中包含每月的每一天:
var d=新日期();
d=新日期(d.getFullYear(),d.getMonth(),1);
var月;
函数setMonth(){
月份=[];
对于(var i=1;i<新日期(d.getFullYear(),d.getMonth(),0).getDate()+1;i++){
var _d=新日期(d);
月[i-1]=新日期(_d.setDate(_d.getDate()-_d.getDay()+i));
}
}
setMonth();
控制台日志(月)代码>这是我的解决方案;应该是不言自明的:
const假日=[
[7,4],//7月4日
[10,31]//万圣节
];
var d=新日期();
var currentDay=d.getDate();
var year=d.getYear()+1900;
var month=d.getMonth();
var合计=0;
var done=0;
对于(var日=1;日-月)休息;//一个月少于31天
如果(t.getDay()==0 | | t.getDay()==6)继续;//无工作日
如果(假期。一些(h=>h[0]-1==月份和h[1]==天))继续;//假日
总计++;//增加总数
if(t.getDate()如果输入2018年7月1日的第一天,则返回所有工作日。它不包括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()
*
[
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
每周的功能日(日期){
让平日=[“星期日”,
“星期一”,
"星期二",,
“星期三”,
"星期四",,
“星期五”,
“星期六”]
返回工作日[date.getDay()]
}
函数格式日期(日期){
设d=新日期(日期)
let month=''+(d.getMonth()+1)
let day=''+d.getDate()
let year=d.getFullYear()
如果(月长<2)月='0'+月
如果(日长<2)天='0'+天
return[年、月、日].加入('-'))
}
函数计算工作日(输入日期、节假日){
Date.prototype.daysPerMonth=函数(){
设d=新日期(this.getFullYear(),this.getMonth()+1,0)
返回d.getDate()
}
let date=新日期(inputDate)
让transitionDays=date.daysPerMonth()
让工作日=[]
for(设i=1;i 我将尝试通过一步一步地解决这个问题,并使每个步骤尽可能地简单来解决这个问题
//如果日期是星期天,则返回true
函数为工作日(日期){
const day=date.getDay();
返回日期!=0&&day!=6;
}
//获取一个月内所有工作日的日期
函数getWeekdaysInMonth(月,年){
返回getDaysInMonth(月,年)。筛选器(isWeekday)
}
//获取一个月内所有日期的日期
函数getDaysInMonth(月,年){
const firstDay=新日期(年、月、1);
const lastDay=新日期(年、月+1,0);
返回getDaysInRange(第一天,最后一天);
}
//获取两个日期之间的日期
函数getDaysInRange(开始、结束){
常数r=[];
对于(让dt=start;dt谢谢!这个方法很有效。接下来的问题-可以编写什么来包括闰年和政府假期?我需要为每个变量设置变量并对这些变量进行for循环检查吗?@apexturtle代码已经计算了闰年,但为了不计算假期,您必须添加一个它们的数组然后相应地比较日期。复活节之类的节日没有固定的日期,这会有点复杂,但可能从一些API中获取这些日期。明白了。谢谢!我会看看我能做些什么。
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()
*
[
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