Hướng dẫn get first tuesday of month javascript - có được thứ ba đầu tiên của tháng javascript

Tôi biết rằng điều này là cũ nhưng đây là một chức năng mà tôi đã viết dựa trên những gì Sahal viết chấp nhận một vài tính năng bổ sung. Tôi đi qua các đối số và trả lại dưới đây.

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 [];
    }
}

Vì vậy, đối số duy nhất được yêu cầu là chuỗi ngày, tùy chọn bạn có thể đặt tháng, năm, đầu tiên hoặc tất cả trong một tháng và tất cả trong một năm hoặc không, tháng và năm mặc định là hiện tại, và đầu tiên và allinyear mặc định là sai, nhưng bạn có thể Đặt đầu tiên trong Moth, bằng cách chuyển null hoặc không xác định cho tham số tháng và năm.

Thông số tháng chấp nhận: null|undefined, number, or string eg 'July'

tham số năm chấp nhận: null|undefined, number 2 or 4 digit, string eg '19' or '2019' also 'last', 'this', 'next'

Trả về đối tượng ngày, mảng [đối tượng ngày ...] hoặc mảng [mảng [đối tượng ngày ...] ...]

Điều này đã được thử nghiệm và nên bao gồm hầu hết các tình huống ...

Làm thế nào để có được ngày đầu tiên của tháng trong JavaScript?

getlyear (), date.getMonth () + 1, 0);Ở đây, một ngày đối tượng đang được tạo sẽ gọi các phương thức được chỉ định trong mã.Chúng tôi một lần nữa tạo một đối tượng ngày mới để có được ngày đầu tiên của tháng hiện tại. getMonth() + 1, 0); Here, an object date is being created which will call the methods specified in the code. We again create a new date object firstDay to get the first day of the current month.

Làm thế nào để bạn tìm thấy thứ Hai đầu tiên của tháng ở JavaScript?

Nhận được thứ hai đầu tiên của tháng JavaScript..
ngày var = ngày mới () ;.
var firstday = ngày mới (ngày. getlyear (), ngày. getMonth (), 1) ;.
var cuối cùng = ngày mới (ngày. getlyear (), ngày. getMonth () + 1, 0) ;.

Làm thế nào để bạn biết nếu đó là ngày đầu tiên của tháng?

Để kiểm tra xem một ngày là ngày đầu tiên của tháng: Sử dụng phương thức getDate () để có được ngày trong tháng cho ngày nhất định. Nếu kết quả bằng 1, ngày là ngày đầu tiên của tháng.Use the getDate() method to get the day of the month for the given date. If the result is equal to 1 , the date is the first day of the month.