My case scenario is User selects a financial year say 1-Jan-2015 to 31-Dec-2015. Then he selects days say 'SATURDAY','SUNDAY'.
I want to fetch all the dates between the above criteria.
I can get the dates like this :-
....
for (var date = fromDate; date <= toDate; date = date.AddDays(1))
temp.Add(date);
But How to get only days 'SATURDAY', 'SUNDAY' from the date range?
Use DayOfWeek property:
for (var date = fromDate; date <= toDate; date = date.AddDays(1))
{
if (date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday)
temp.Add(date);
}
In case if set of days of week is not static:
HashSet<DayOfWeek> daysOfWeek = new HashSet<DayOfWeek> { DayOfWeek.Saturday, DayOfWeek.Sunday };
List<DateTime> temp = new List<DateTime>();
for (var date = fromDate.Date; date <= toDate; date = date.AddDays(1))
if (daysOfWeek.Contains(date.DayOfWeek))
temp.Add(date);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments