473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7824
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

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

Similar topics

10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
3
by: Tom Meerstad | last post by:
Hello, I am using a calender which begins with a sunday, but I would like it to begin with monday. The author's mail address isn't working (mail got bounced) so I'm really hoping anyone here...
8
by: MLH | last post by:
Anybody's solution would be appreciated. Pls, do not pause to write anything for this. I'm not looking for that kind of a handout. I have an idea about how to do it, but I wanted to see if anyone...
2
by: MLH | last post by:
With a table of holidays and A97's date fn's - how best to count weekends and holidays between two dates? My holiday table has 4 fields. I will be adding records to it each year as info becomes...
4
nirmalsingh
by: nirmalsingh | last post by:
hi all! i have to find the dates of sundays in a year. help me with sample code in C#. thanx in advance.
1
by: karunajo | last post by:
hi, I am giving two dates as input.Iwant to calculate the number of days between those days except sundays.How can i do it.. Help me..
1
by: burnspaul | last post by:
I need to input a date into one field and have access show me a another date (plus 20 days) in another field (which I can do) but here is where I need help. I need to exclude Saturdays & Sundays...
3
by: iceone | last post by:
Hi everybody, I am monitoring a call center and i need to calculate the working hours between the moment the call is answered and the moment the call is closed. i need to calculate the working...
1
by: Pradeepa01 | last post by:
Hi, We are facing an issue with the JDBC query, I need help on. I have 3 fields, UPC_ID, QTY,Date I have to retrieve all the records for one UPC_ID whose QTY is same for last five days. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.