Get the week number of a given date

by Raju Maharjan 19. June 2011 22:27

 

It seems there is no Week property on a DateTime instance. So I started looking for a way to get the week number. As it turns out the CultureInfo class will get you where you want to go using the Calendar property.

The C# method uses the DateTime object to get the week number of a given date. The number returned is dependent on the culture of the target machine and which will be the week number for the provided date.


  1. public static int GetWeekNumber(DateTime dtSelectedDate)
  2. {
  3.         CultureInfo ciCurr = CultureInfo.CurrentCulture;
  4.         int weekNum = ciCurr.Calendar.GetWeekOfYear(dtSelectedDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
  5.         return weekNum;
  6. }

 

Tags:

.NET Programming Tips | C#

Comments

Add comment


() (Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading




Sapnep IT Solution

We Provide IT solutions and services to deliver a unique blend of real-business experience with a strong engineering, international project delivery. We are a software development  firm.