C#: Get Week Number

This gets the week number. Be careful as not all week numbers are the same! .NET/C#

C# Code Snippets

C#: Get Week Number

This gets the week number. Be careful as not all week numbers are the same! Read our article How Difficult are WEEK NUMBERS?

public static int GetWeekNumber(DateTime thisDate, int weekStartsOn = 0)
{
  System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;

  int weekNum = cul.Calendar.GetWeekOfYear(
      thisDate,
      System.Globalization.CalendarWeekRule.FirstFourDayWeek,
      (DayOfWeek)weekStartsOn); // we can set this

  return weekNum;
}