如何使用Javascript查找当月的工作日? [英] How to find business days in current month with Javascript?

查看:116
本文介绍了如何使用Javascript查找当月的工作日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建用于查找当月工作日数的功能?您可以在没有Jquery的情况下使用简单的javascript进行编码.

How can i create function for finding numbers of business days of current month? Can you code in simple javascript without Jquery.

    function daysInMonth(iMonth, iYear)
    {
    return 32 - new Date(iYear, iMonth, 32).getDate();
    }

    function Detail() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var dim = daysInMonth(month, year);
    alert(dim);
    }
    function Businessday(iMonth, iYear)
    {
   //   enter code here
    } 
    function isBusinessDay(){
    var d=new Date();
    var day = d.getDay();
    switch(day) {
        case 0:
            document.write("Today is Weekend");
        break;
        case 6:
        document.write("Today is Weekend");
            break;
            default:
            document.write("Today is business day");
        }
           }

先谢谢了.

推荐答案

好的,让我们一次解决这一问题.

OK, let's solve this one piece at a time.

JavaScript中的 Date 对象具有方法 getDay .星期日返回0,星期一返回1,星期二返回2,星期六返回6.鉴于此,我们可以得出结论,我们不想计算谁的 getDay 返回0或6的天数.

The Date object in JavaScript has a method getDay. This will return 0 for Sunday, 1 for Monday, 2 for Tuesday, ... 6 for Saturday. Given that, we can conclude that we want to not count days whos getDay returns 0 or 6.

您已经有一个函数可以返回一个月中的天数,因此假设这样,我们可以遍历所有天数并检查getDay的结果. daysInMonth 假设月份是从零开始的;所以0 =一月.

You already have a function to return the number of days in a month, so assuming that, we can loop over all of the days and check the result of getDay. daysInMonth makes the assumption that the month is zero based; so 0 = January.

我鼓励您尝试从此处自行解决此问题;否则请继续阅读.

I'd encourage you to try solving this on your own from here; otherwise read on.

让我们从 isWeekday 函数开始.我们需要年,月和日:

Let's start with an isWeekday function. We need the year, month, and day:

function isWeekday(year, month, day) {
var day = new Date(year, month, day).getDay();
return day !=0 && day !=6;
}

我们完全按照上面的讨论进行操作:我们构造一个Date,然后使用 getDay 来确定是否是一天.

We do exactly as we talked about above: we construct a Date, and use getDay to determine if it's a day.

现在我们需要遍历该月的所有天:

Now we need to loop over all of the days in the month:

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++;
}
return weekdays;
}

我们遍历一个月中的所有日子.我们在检查 isWeekday 时将其加1,因为与月份不同的是,日期是基于1的.如果是,我们将增加工作日,然后返回.

We loop over all of the days in the month. We add 1 when checking isWeekday because the day, unlike month, is 1 based. If it is, we increment weekdays, then return.

所以我们可以像这样使用 getWeekdaysInMonth :

So we can use getWeekdaysInMonth like this:

var weekdays = getWeekdayInMonth(9, 2011); //9 = October.

这将导致21.

这篇关于如何使用Javascript查找当月的工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆