All of these looks like a homework to me.
In the real life this one better to be done using calendar table.
And you would only use the script to populate it...
Also, there are a calendar months and weeks and there are financial months and weeks. What is required in your case?