So if you were to run this on the, say, the 30th of November, you'd actually be using the 1st of November as your start date, since the 30th's a Monday. If you run it on a Sunday, getDay
will return zero, meaning your start date will be the last of the previous month. And so on.
I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map
to dates. The simpler way looping through incrementing numbers is a plain old for
-loop.
Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time (aka winter/summer time) can cause all sorts of nonsense when mixing raw time and datedates. Some calendar days are 25 hours long, some are 23. And since a date constructed from only year, month, and date sets its time to midnight, you risk skipping over a date, or repeating the same date twice.
function remainingDays(weekday) {
var current = new Date,
year = current.getFullYear(),
dates = [];
// a simple helper function
function nextDay(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
}
// as long we're in the same year, keep adding 1 day,
// and store the ones that match the wdayweekday we're looking for
while(current.getFullYear() === year) {
if(current.getDay() === weekday) {
dates.push(current);
}
current = nextDay(current);
}
return dates;
}