Click here to Skip to main content
13,550,059 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 14 Jul 2011
Licenced CPOL

DateTime Extension Method to Give Week Number

, 14 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Strangely, the .NET DateTime does not provide a Week Number. This extension rectifies this.


Week number is quite complex: ISO 8601[^] specifies that weeks start on Monday, and that Jan 1st is in week one if it occurs on a Thursday, Friday, Saturday, or Sunday. Otherwise it is in the last week of the previous year, which may be week 51, 52, or 53.

Over the years, I have come up with a couple of ways of handling this, but this version uses recursion to identify the last week of the previous year, and is pretty simple to understand.

public static class ExtensionMethods
    /// <summary>
    /// Offsets to move the day of the year on a week, allowing
    /// for the current year Jan 1st day of week, and the Sun/Mon 
    /// week start difference between ISO 8601 and Microsoft
    /// </summary>
    private static int[] moveByDays = { 6, 7, 8, 9, 10, 4, 5 };
    /// <summary>
    /// Get the Week number of the year
    /// (In the range 1..53)
    /// This conforms to ISO 8601 specification for week number.
    /// </summary>
    /// <param name="date"></param>
    /// <returns>Week of year</returns>
    public static int WeekOfYear(this DateTime date)
        DateTime startOfYear = new DateTime(date.Year, 1, 1);
        DateTime endOfYear = new DateTime(date.Year, 12, 31);
        // ISO 8601 weeks start with Monday 
        // The first week of a year includes the first Thursday 
        // This means that Jan 1st could be in week 51, 52, or 53 of the previous year...
        int numberDays = date.Subtract(startOfYear).Days + 
        				moveByDays[(int) startOfYear.DayOfWeek];
        int weekNumber = numberDays / 7;
        switch (weekNumber)
            case 0:
                // Before start of first week of this year - in last week of previous year
                weekNumber = WeekOfYear(startOfYear.AddDays(-1));
            case 53:
                // In first week of next year.
                if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
                    weekNumber = 1;
        return weekNumber;

It works by calculating the number of days since the start of the year, and moving that forward by a week to get a one-based answer. This is offset back a week, if the start of the year is a Monday, Tuesday or Wednesday, and adjusted for the DateTime.DayOfWeek return of Sunday as the week start - hence the odd numbering of the moveByDays array elements.


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


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Thanks! Another example of how we co... Pin
Walt Fair, Jr.15-Jul-11 7:14
subeditorWalt Fair, Jr.15-Jul-11 7:14 
Reason for my vote of 5
Thanks! Another example of how we computer people can complicate a simple concept!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid