Originally Posted by felgall
There's only one thing really wrong with the original code:
This sets the date to the last day of the previous month (why you were getting December instead of January) - to set to the last day of the current month you'd need to add 1 to the month as well.
In which case it needs to be
d.setMonth(d.getMonth() + 1);
lastsunday = d.getDate() - (d.getDay());