Months in javascript Date objects are returned as the integers 0-11,
with 0 being January, 1 February, and so on.
Weekdays are also 0 based- Sunday is 0, Monday is 1.
To return an Array of all of the Sundays in a given year-
1. Create a date object for January 1 of that year.
2. If the weekday (Date.getDay()) is NOT Sunday (0), set the Date to the next date that is .
This gives you a Date object for the first Sunday of the year, which is the first member of your array.
3. Fill the Array by repeatedly adding 7 to the date, until the next Sunday falls in the next year.
- function getSundays(y){
-
y=y|| new Date().getFullYear();
-
var A=[];
-
var D=new Date(y,0,1)
-
var day= D.getDay();
-
if(day!=0) D.setDate(D.getDate()+(7-day));
-
A[0]=D.toLocaleString();
-
while(D){
-
D.setDate(D.getDate()+7);
-
if(D.getFullYear()!=y) return A;
-
A.push(D.toLocaleDateString());
-
}
-
}
What you save in the Array is up to you-
this example saves a locally formatted string,
but you could extract just the month and date-
A.push((D.getMonth()+1+' '+D.getDate()).