I am trying to get all the weekdays of the selected Month along with the day like Monday or Tuesday, etc?? How is it possible? is there an inbuilt function that i do not know?
This is in C# 2.0, asp.net using VS 2005
I make no guarantees about leap years, daylight savings jumps, special days, Shanghai 1927, etc.
public List<DateTime> getWeekdatesandDates(int Month, int Year)
{
List<DateTime> weekdays = new List<DateTime>();
DateTime firstOfMonth = new DateTime(Year, Month, 1);
DateTime currentDay = firstOfMonth;
while (firstOfMonth.Month == currentDay.Month)
{
DayOfWeek dayOfWeek = currentDay.DayOfWeek;
if (dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday)
weekdays.Add(currentDay);
currentDay = currentDay.AddDays(1);
}
return weekdays;
}
The resultant DateTime
objects have a DayOfWeek
property which you can check to see if it's Monday through Friday.
This should work
private List<DateTime> getWeekDayDates(int month, int year)
{
List<DateTime> weekdays = new List<DateTime>();
DateTime basedt = new DateTime(year, month, 1);
while ((basedt.Month == month) && (basedt.Year == year))
{
if (basedt.DayOfWeek == (DayOfWeek.Monday | DayOfWeek.Tuesday | DayOfWeek.Wednesday | DayOfWeek.Thursday | DayOfWeek.Friday))
{
weekdays.Add(new DateTime(basedt.Year, basedt.Month, basedt.Day));
}
basedt = basedt.AddDays(1);
}
return weekdays;
}
Then you can get whatever info out of each DateTime you need.