469,347 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

how to find dates of sundays in javascript?

nirmalsingh
218 100+
hi all!

I want to find dates of sundays in a particular year. help me with sample code. thanx in advance.
May 8 '07 #1
6 6967
mrhoo
428 256MB
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.
Expand|Select|Wrap|Line Numbers
  1. function getSundays(y){
  2.     y=y|| new Date().getFullYear();
  3.     var A=[];
  4.     var D=new Date(y,0,1)
  5.     var day= D.getDay();
  6.     if(day!=0) D.setDate(D.getDate()+(7-day));
  7.     A[0]=D.toLocaleString();
  8.     while(D){
  9.         D.setDate(D.getDate()+7);
  10.         if(D.getFullYear()!=y) return A;
  11.         A.push(D.toLocaleDateString());
  12.     }
  13. }
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()).
May 8 '07 #2
nirmalsingh
218 100+
hai all,
i want to find the dates of sundays in a year. how to do this? and how to find what day is 9 may 2007?
i need the result as Wednesday.
May 9 '07 #3
acoder
16,027 Expert Mod 8TB
Threads merged because they were on the same topic.
May 9 '07 #4
acoder
16,027 Expert Mod 8TB
hai all,
i want to find the dates of sundays in a year. how to do this? and how to find what day is 9 may 2007?
i need the result as Wednesday.
See the post by mrhoo for the first question.

For 9 May 2007, set the date using the methods of the Date object - see link Then use the getDay() ethod to get the day.
May 9 '07 #5
hi, How to Find The Sturdays And Sundays In between the given two days.
May 17 '07 #6
acoder
16,027 Expert Mod 8TB
hi, How to Find The Sturdays And Sundays In between the given two days.
Sunday is 0, so Saturday is 6. Just look for those two and keep adding 6 and 1 to get the weekend days until you get past the second date.
May 17 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Colin Steadman | last post: by
3 posts views Thread by Tom Meerstad | last post: by
1 post views Thread by burnspaul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.