user1031746
user1031746

Reputation: 361

Week number of a year in dateformat

I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.

The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?

Upvotes: 12

Views: 19725

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460268

There's no format that provides the weeknumber, but you can get it in this way easily:

System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
    DateTime.Now, 
    System.Globalization.CalendarWeekRule.FirstFourDayWeek, 
    DayOfWeek.Monday);

To answer your comment how to get the quarter of a year of a given DateTime:

int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1

Upvotes: 24

Related Questions