///
/// for example if you pass if year is 2009
/// it will return all the dates of sundays in the year in form of list
///
/// Year
///
public List
{
List
for (int month = 1; month <= 12; month++)
{
DateTime dt = new DateTime(Year, month, 1);
int firstSundayOfMonth = (int)dt.DayOfWeek;
if (firstSundayOfMonth != 0)
{
dt = dt.AddDays((6 - firstSundayOfMonth) + 1);
}
while (dt.Month == month)
{
strDates.Add(dt.ToString("dd/MMM/yyyy"));
dt = dt.AddDays(7);
}
}
return strDates;
}
Testing above function
List
DropDownList1.DataSource = allSundays;
DropDownList1.DataBind();
Hope this helps
Tags: .NET, C# DateTime, All Sundays in a Year
No comments:
Post a Comment