I want a method that gives me the list of days for all weeks of a year.
I found this interesting code form this link : Getting the list of days of the current week from DateTime.Now
DateTime today = DateTime.Today;
int currentDayOfWeek = (int) today.DayOfWeek;
DateTime sunday = today.AddDays(-currentDayOfWeek);
DateTime monday = sunday.AddDays(1);
// If we started on Sunday, we should actually have gone *back*
// 6 days instead of forward 1...
if (currentDayOfWeek == 0)
{
monday = monday.AddDays(-7);
}
var dates = Enumerable.Range(0, 7).Select(days => monday.AddDays(days)).ToList();
Code from Jon Skeet
Can we generalize this case to treat every week in a given year ?
This is my actual code, inspired from Jon Skeet's one :
public static List<DateTime> JoursSemaine()
{
int currentDayOfWeek = (int)maintenant.DayOfWeek;
DateTime sunday = maintenant.AddDays(-currentDayOfWeek);
DateTime monday = sunday.AddDays(1);
// Si on commence un dimanche, on recule de 6 jours au lieu d'en avancer d'un
if (currentDayOfWeek == 0)
{
monday = monday.AddDays(-7);
}
var dates = Enumerable.Range(0, 7).Select(days => monday.AddDays(days)).ToList();
lundi = dates[0];
mardi = dates[1];
mercredi = dates[2];
jeudi = dates[3];
vendredi = dates[4];
return dates;
}
This actually gives me the list of the day (from monday to friday) for the actual week.
I want to use the week number to access to the list of days of the choosen week.