dates | | Convert a date to ISO | Search

This code identifies and lists all Tuesdays in the remaining months of 2017 that occur on or after the 26th of the month. It does this by calculating the first Tuesday of each month and iterating through subsequent Tuesdays until the next month begins.

Run example

npm run import -- "find Tuesdays late in the month"

find Tuesdays late in the month

function getTuesdays(month, year) {
    var d = new Date(year, month, 1),
        tuesdays = [];

    d.setDate(d.getDate() + (9 - d.getDay()) % 7)
    while (d.getMonth() === month) {
        tuesdays.push(new Date(d.getTime()));
        d.setDate(d.getDate() + 7);
    }

    return tuesdays;
}

var month = (new Date()).getMonth();
var results = [];
for (var m = month; month <= 12; month++) {
    results = results.concat(getTuesdays(month, 2017).filter((d) => d.getDate() > 25));
}
console.log(results.map(d => d.getDate() + '/' + (d.getMonth() + 1)).join('\r\n'))
module.exports = getTuesdays;

What the code could have been:

/**
 * Calculates all Tuesdays in a given month and year.
 * 
 * @param {number} month - Zero-based month (0-11)
 * @param {number} year - Four-digit year
 * @returns {Date[]} Array of Tuesdays in the given month
 */
function getTuesdays(month, year) {
    // Initialize the first day of the month
    const firstDay = new Date(year, month, 1);
    
    // Calculate the first Tuesday of the month
    let tuesday = new Date(firstDay.getTime());
    tuesday.setDate(tuesday.getDate() + (9 - tuesday.getDay()) % 7);
    
    // Initialize an array to store Tuesdays
    const tuesdays = [];
    
    // Loop through each week in the month
    while (tuesday.getMonth() === month) {
        // Add the current Tuesday to the array
        tuesdays.push(new Date(tuesday.getTime()));
        
        // Move to the next Tuesday
        tuesday.setDate(tuesday.getDate() + 7);
    }
    
    return tuesdays;
}

/**
 * Filters Tuesdays that occur after the 25th day of the month.
 * 
 * @param {Date[]} tuesdays - Tuesdays in a given month
 * @returns {Date[]} Tuesdays that occur after the 25th day of the month
 */
function filterTuesdaysAfter25(tuesdays) {
    return tuesdays.filter((tuesday) => tuesday.getDate() > 25);
}

/**
 * Prints Tuesdays after the 25th day of each month in a given year.
 */
function printTuesdaysAfter25() {
    // Get the current month
    const currentMonth = (new Date()).getMonth();
    
    // Initialize an array to store the result
    const results = [];
    
    // Loop through each month in the year
    for (let month = currentMonth; month <= 11; month++) {
        // Get the Tuesdays in the current month
        const tuesdays = getTuesdays(month, 2017);
        
        // Filter out Tuesdays that occur after the 25th day
        const filteredTuesdays = filterTuesdaysAfter25(tuesdays);
        
        // Add the filtered Tuesdays to the result array
        results.push(...filteredTuesdays);
    }
    
    // Print the result in the desired format
    console.log(results.map((tuesday) => `${tuesday.getDate()}/${tuesday.getMonth() + 1}`).join('\n'));
}

// Export the function
module.exports = { getTuesdays, filterTuesdaysAfter25, printTuesdaysAfter25 };

This code finds all Tuesdays in the remaining months of 2017 that fall on or after the 26th.

Here's a breakdown:

  1. getTuesdays(month, year) function:

  2. Main Logic:

  3. Output:

Let me know if you'd like a more detailed explanation of any specific part!