How to find the dates of the last Fridays of each month of a given year in JavaScript

1 Answer

0 votes
// Return all last Fridays of each month in a given year
function* lastFridaysOfYear(year) {
    for (let month = 1; month <= 12; month++) {

        // Last day of the month
        let date = new Date(year, month, 0); // day 0 of next month = last day of this month

        // Walk backward to Friday
        while (date.getDay() !== 5) { // 5 = Friday
            date.setDate(date.getDate() - 1);
        }

        yield date;
    }
}

function main() {
    const args = process.argv.slice(2);

    const year = args.length > 0
        ? parseInt(args[0], 10)
        : 2026;

    for (const date of lastFridaysOfYear(year)) {
        console.log(
            date.toLocaleDateString("en-US", { timeZone: "UTC" })
        );
    }
}

main();


/*
run:

1/30/2026
2/27/2026
3/27/2026
4/24/2026
5/29/2026
6/26/2026
7/31/2026
8/28/2026
9/25/2026
10/30/2026
11/27/2026
12/25/2026

*/

 



answered May 23 by avibootz

Related questions

...