Search This Blog

Friday, January 15, 2021

Find Last 5 Weeks Number in C#

public static int GetIso8601WeekOfYear(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(timeCalendarWeekRule.FirstFourDayWeekDayOfWeek.Monday);
}

 int week1 = GetIso8601WeekOfYear(DateTime.Today);

int week2 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 6));
int week3 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 12));
int week4 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 18));
int week5 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 21));

No comments:

Post a Comment