Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Created August 2, 2020 15:23
Show Gist options
  • Save indiejoseph/1c9db1515daab82c17972ff5c13cb35f to your computer and use it in GitHub Desktop.
Save indiejoseph/1c9db1515daab82c17972ff5c13cb35f to your computer and use it in GitHub Desktop.
Find a possible years for the given month date and weekday
function getYearByMonthDateAndWeekday (month, date, weekday) {
const now = new Date();
const startYear = 2013;
const endYear = now.getFullYear();
const possibleYears = Array.from(new Array((endYear - startYear) + 1)).map((_, i) => startYear + i).sort((a, b) => b - a);
const validYears = possibleYears.filter((year) => {
const thisYear = new Date(year, month - 1, date);
return thisYear.getDay() === weekday;
});
return validYears[0];
}
console.log(getYearByMonthDateAndWeekday(8, 2, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment