Created
August 2, 2020 15:23
-
-
Save indiejoseph/1c9db1515daab82c17972ff5c13cb35f to your computer and use it in GitHub Desktop.
Find a possible years for the given month date and weekday
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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