ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

WeekOfYearISO8601

Gets the number of the week according to the definition of the ISO 8601

Source

public static int WeekOfYearISO8601(this DateTime date)
{
    var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
    var week = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

    return week;
}

Example

int weekNumber = DateTime.Now.WeekOfYearISO8601();

Author: João Mata

Submitted on: 1 dec. 2016

Language: C#

Type: string

Views: 3505