How to get week number of the year – C#

Getting the week number of the year from the DateTime object can be implemented proprietary-ly but Microsoft does provide a way to get this calculated with a built-in method.

Here is the way you can achieve this:

static void Main(string[] args)    
{
  DateTime inputDate = DateTime.Now;    
 
  CultureInfo currentCulture = CultureInfo.CurrentCulture;    

  var weekNum = currentCulture.Calendar.GetWeekOfYear(    
    inputDate,    
    CalendarWeekRule.FirstDay,    
    DayOfWeek.Monday);    

  Console.WriteLine("Year: {0} Week: {1}", inputDate.Year, weekNum);
}