每个月的第一个星期二

时间:2013-01-25 09:41:40

标签: javascript

您好我正在尝试使用以下代码,这是一个“提升代码”的混搭,但它一直在抽出今天的日期和时间。

我想把每个月的第一个星期二的日期定在19:00。

我正在使用W3C School Try-it进行测试。

<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{

var myDate = new Date();
myDate.setHours(19, 00, 0, 0);
myDate.setYear(2013);

myDate.setDate(1);
myDate.setMonth();


//Find Tuesday
var tue = 2;

while(myDate.getDay() != tue) {
    myDate.setDate(myDate.getDate() + 1);

}

document.write(myDate);
</script>
</head>
<body>

<h1>My First JavaScript</h1>
<p id="demo">This is a paragraph.</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html> 

罗伊

&LT; ==更新==&GT;

我已经使用了你的代码,该代码运行良好,但需要在下个月第一个星期二发生之后考虑因素,我已经尝试了这个if语句但是休息了。

function getTuesday() {
    var datenow = new Date();
    var d = new Date(),
        month = d.getMonth(),
        tuedays= [];

    d.setDate(1);

    // Get the first Monday in the month
    while (d.getDay() !== 2) {
        d.setDate(d.getDate() + 1);
    }

    // Get all the other Tuesdays in the month
    while (d.getMonth() === month) {
        tuedays.push(new Date(d.setHours(17,00,00,00)));
        d.setDate(d.getDate() + 7);
    }
If (d.getDate() >= datenow.getDate)
{
    d.setMonth(d.getMonth() + 1);
    document.write(tuedays[1]);
}
Else
{
   document.write(tuedays[1]);
}


}

2 个答案:

答案 0 :(得分:1)

使用以下功能,这将为您提供当前周二的月份。

function getTuesday() {
    var d = new Date(),
        month = d.getMonth(),
        tuedays= [];

    d.setDate(1);

    // Get the first Monday in the month
    while (d.getDay() !== 2) {
        d.setDate(d.getDate() + 1);
    }

    // Get all the other Tuesdays in the month
    while (d.getMonth() === month) {
        tuedays.push(new Date(d.getTime()));
        d.setDate(d.getDate() + 7);
    }

    return tuedays;
}

答案 1 :(得分:0)

我知道这很旧,但这是我根据Sahal编写的功能编写的功能,该功能接受了一些其他功能。生病的参数,然后返回下面。

function getDates(dayString, month, year, first, allInYear) {
    if (!dayString) { console.error('Missing required parameter: dayString is required.'); return; }
    if (first === undefined || first === null) { first = false; }
    if (allInYear === undefined || allInYear === null) { allInYear = false; }
    if (year === undefined || year === null) { year = new Date().getFullYear(); }
    var converted = false;
    if (month === undefined || month === null) {
        var temp = new Date();
        if (temp.getDate() > 9) {
            month = ((temp.getMonth() + 1) == 12) ? 11 : (temp.getMonth() + 1);
        } else {
            month = temp.getMonth();
        }
        converted = true;
    }
    if (typeof month === "string" && isNaN(parseInt(month))) {
        month = month.toLowerCase().substring(0, 3);
        switch (month) {
            case "jan":
                month = 0;
                break;
            case "feb":
                month = 1;
                break;
            case "mar":
                month = 2;
                break;
            case "apr":
                month = 3;
                break;
            case "may":
                month = 4;
                break;
            case "jun":
                month = 5;
                break;
            case "jul":
                month = 6;
                break;
            case "aug":
                month = 7;
                break;
            case "sep":
                month = 8;
                break;
            case "oct":
                month = 9;
                break;
            case "nov":
                month = 10;
                break;
            case "dec":
                month = 11;
                break;
            default:
                var temp = new Date();
                if (temp.getDate() > 9) {
                    month = ((temp.getMonth() + 1) == 12) ? 11 : (temp.getMonth() + 1);
                } else {
                    month = temp.getMonth();
                }
                converted = true;
                break;
        }
    } else if (typeof month === "number" || (typeof month === "string" && !isNaN(parseInt(month)))) {
        month = (!converted) ? parseInt(month) - 1 : month;
    }
    if (typeof year === "string" && !isNaN(parseInt(year)) || typeof year === "number") {
        if (parseInt(year) / 1000 > 2) {
            year = parseInt(year);
        } else if (parseInt(year) / 10 >= 0 && parseInt(year) / 10 < 10) {
            var temp2 = new Date().getFullYear();
            year = (parseInt(Math.floor(temp2 / 100)) * 100) + parseInt(year);
        }
    } else if (typeof year === "string" && isNaN(parseInt(year))) {
        if (year === "this") {
            year = new Date().getFullYear();
        } else if (year === "last") {
            year = new Date().getFullYear() - 1;
        } else if (year === "next") {
            year = new Date().getFullYear() + 1;
        } else {
            console.warn('Year string not recognized, falling back to current year. (this, last, next).');
            year = new Date().getFullYear();
        }
    }
    var dates = [];
    //hang in there this is going to get a doosie
    var d = new Date(),
        dow = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
        getDow = function(sd, dowa) {
            for (var i = 0; i < dowa.length; i++) {
                var day = dowa[i];
                if (sd.toLowerCase().substring(0, 3) == day) {
                    return i;
                }
            }
            return -1;
        },
        di = getDow(dayString, dow),
        getDIM = function(year, mon) {
            var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
            return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][mon];
        };
    d.setFullYear(year);
    d.setMonth(month, 1);
    if (di == -1) { console.error('Range Error: Day of the week should be between sunday and saturday'); return; }
    if (first && !allInYear) {
        while (d.getDay() !== di) {
            d.setDate(d.getDate() + 1);
        }
        return d;
    } else if (first && allInYear) {
        var tm = 0;
        d.setMonth(tm, 1);
        for (var i = tm; i <= 11; i++) {
            while (d.getDay() !== di) {
                d.setDate(d.getDate() + 1);
            }
            dates.push(new Date(d));
            tm += 1;
            d.setMonth(tm, 1);
        }
        return dates;
    } else if (!first && !allInYear) {
        var eom = getDIM(d.getFullYear(), d.getMonth());
        for (var x = 1; x <= eom; x++) {
            if (d.getDay() === di) {
                dates.push(new Date(d));
            }
            d.setDate(d.getDate() + 1);
        }
        return dates;
    } else if (!first && allInYear) {
        var tm = 0;
        for (var i = 0; i <= 11; i++) {
            var eom = getDIM(d.getFullYear(), i),
                dim = [];
            d.setMonth(i, 1);
            for (var x = 1; x <= eom && d.getMonth() == i; x++) {
                if (d.getDay() === di) {
                    dim.push(new Date(d));
                }
                d.setDate(d.getDate() + 1);
            }
            dates.push(dim);
        }
        return dates;
    } else {
        return [];
    }
}

因此,唯一需要的参数是天字符串,您可以选择将月,年,月中的第一个或全部以及是否在一年中的所有月,年和月默认设置为当前,而first和allInYear默认设置为false,但是您可以通过将null或undefined传递给month和year参数来设置优先。

month参数接受:null|undefined, number, or string eg 'July'

year参数接受:null|undefined, number 2 or 4 digit, string eg '19' or '2019' also 'last', 'this', 'next'

返回Date对象,Array [日期对象...]或Array [Array [日期对象...] ...]

这已经过测试,应该可以涵盖大多数情况...