Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Respected Sir,
I have made an application in which I want to find the sunday between the given month. I have take two textbox like textbox1 & textbox2. First textbox take date like 01/08/2012 & 2nd textbox taken date like 31/08/2012. So find the sunday between this date.
So Please Sir, given me the proper answer of my quesion.


Thanks
Nilesh Gujar
Posted

1 solution

Try this:
C#
// The year (1 through 9999).
// The month (1 through 12).
static int CountSundays(int year, int month)
{
	var firstDay = new DateTime(year,month , 1);

	var day29 = firstDay.AddDays(28);
	var day30 = firstDay.AddDays(29);
	var day31 = firstDay.AddDays(30);

	if ((day29.Month == month && day29.DayOfWeek == DayOfWeek.Sunday)
	|| (day30.Month == month && day30.DayOfWeek == DayOfWeek.Sunday)
	|| (day31.Month == month && day31.DayOfWeek == DayOfWeek.Sunday))
	{
		return 5;
	}
	else
	{
		return 4;
	}
}

Refer: finding sundays in given month using SQL Server[^]

Similar thread:
How to calculate number of sundays in a year in asp.net 2.0[^]
Lot more on CP[^]

..and many more on google[^]
 
Share this answer
 
Comments
ridoy 21-Aug-12 3:07am    
good solution..+5
Prasad_Kulkarni 21-Aug-12 3:09am    
Thank you ridoy!
Manas Bhardwaj 21-Aug-12 3:59am    
Right +5!
Prasad_Kulkarni 21-Aug-12 4:40am    
Thank You Manas!
gopesh2010 21-Aug-12 5:01am    
Nice solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900