0

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.

1 Answers1

0
public static List<DateTime> GetWeekDaysOfWeekFrom(int weekNumber)
{
    DateTime firstDayOfWeek = FirstDateOfWeekISO8601(DateTime.Today.Year, weekNumber);

    int currentDayOfWeek = (int)firstDayOfWeek.DayOfWeek;
    DateTime sunday = firstDayOfWeek.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);
    }

    return Enumerable.Range(0, 7).Select(days => monday.AddDays(days)).ToList();
}

public static DateTime FirstDateOfWeekISO8601(int year, int weekOfYear)
{
    DateTime jan1 = new DateTime(year, 1, 1);
    int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;

    DateTime firstThursday = jan1.AddDays(daysOffset);
    var cal = CultureInfo.CurrentCulture.Calendar;
    int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

    var weekNum = weekOfYear;
    if (firstWeek <= 1)
    {
        weekNum -= 1;
    }
    var result = firstThursday.AddDays(weekNum * 7);
    return result.AddDays(-3);
}

See also Calculate date from week number

  • It seems that it works. Now i have to work on my drop down list to display the good list of days for the selected value ; but i've got big problems with it ; that's why i ask a new question for it. – Christopher LEBAS Sep 28 '16 at 08:51
  • I've found an anomaly ; last year, we had 53 weeks ; this one overflow on this year with the Friday, 1st of January. For the program, it search the 53rd week of 2016 (that is to say the first of 2017 ; the program return the 6/01/2017 for the wanted friday instead of 1/01/2016), not 2015. How to correct this anomaly ? – Christopher LEBAS Sep 28 '16 at 15:05