Skip to content

Instantly share code, notes, and snippets.

@ezamelczyk
Last active September 20, 2017 14:31
Show Gist options
  • Save ezamelczyk/fc6f83a9fab86c0befbf1cd04347dbd4 to your computer and use it in GitHub Desktop.
Save ezamelczyk/fc6f83a9fab86c0befbf1cd04347dbd4 to your computer and use it in GitHub Desktop.
const readline = require('readline');
const readInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function getLastMondayMonths(startYear, endYear) {
if(!endYear) {
endYear = startYear;
}
if (startYear > endYear) {
console.log("Starting year must be smaller or equal to the ending year.\n");
} else {
let date = new Date(startYear, 1, 0);
let dates = "";
while (date.getFullYear() <= endYear) {
for(let month = 1; month <= 12; month++){
let tempDate = new Date(date.getFullYear(), month, 0);
if(tempDate.getDay() === 1) {
dates += " " + (tempDate.getMonth() + 1) + "/" + tempDate.getDate() + "/" + tempDate.getFullYear();
}
}
date.setFullYear(date.getFullYear() + 1);
}
return dates.substr(1);
}
}
readInterface.question("Type starting year: ", (year) => {
readInterface.question("Type ending year: ", (end) => {
console.log(getLastMondayMonths(year, end));
process.exit();
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment