Click here to Skip to main content
15,892,290 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
How can i Get the week number?

e.g. there are 53 weeks in year and if i check the week numer of 1-March then its 9th week and if i pass the date i.e 8th-march then its 10 week of the year.
Posted

1 solution

As you might have noticed DateTime does not have a WeekNumber property. This however is available inside the CultureInfo calendar :)

So we first create a culture variable and fill it with the current culture.

Then you can first configure some more rules inside the calendar like what day of the week it should concider as the first day.

Here a code example for you :)

Good luck

DateTime dateTime = DateTime.Parse("08-03-2010");

System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CurrentCulture;

int intWeek = culture.Calendar.GetWeekOfYear(dateTime, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900