Get the week number : Date Time Util « Date Time « C# / C Sharp






Get the week number

   

//Octavalent Extension Methods
//http://sdfasdf.codeplex.com/
//Library of extension methods for .Net create by Octavalent (www.octavalent.nl)
using System;

namespace System.OctavalentExtensions
{
    public static class DateTimeExtensions
    {
        #region Week
        public static int GetWeekNumber(this DateTime date)
        {
            // Updated 2004.09.27. Cleaned the code and fixed abug. Compared the algorithm with
            // code published here . Tested code successfully against the other algorithm 
            // for all dates in all years between 1900 and 2100.
            // Thanks to Marcus Dahlberg for pointing out the deficient logic.

            // Calculates the ISO 8601 Week Number
            // In this scenario the first day of the week is monday, 
            // and the week rule states that:
            // [...] the first calendar week of a year is the one 
            // that includes the first Thursday of that year and 
            // [...] the last calendar week of a calendar year is 
            // the week immediately preceding the first 
            // calendar week of the next year.
            // The first week of the year may thus start in the 
            // preceding year

            const int JAN = 1;
            const int DEC = 12;
            const int LASTDAYOFDEC = 31;
            const int FIRSTDAYOFJAN = 1;
            const int THURSDAY = 4;
            bool ThursdayFlag = false;

            // Get the day number since the beginning of the year
            int DayOfYear = date.DayOfYear;

            // Get the numeric weekday of the first day of the 
            // year (using sunday as FirstDay)
            var StartWeekDayOfYear =
                (int) (new DateTime(date.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek;
            var EndWeekDayOfYear =
                (int) (new DateTime(date.Year, DEC, LASTDAYOFDEC)).DayOfWeek;

            // Compensate for the fact that we are using monday
            // as the first day of the week
            if (StartWeekDayOfYear == 0)
                StartWeekDayOfYear = 7;
            if (EndWeekDayOfYear == 0)
                EndWeekDayOfYear = 7;

            // Calculate the number of days in the first and last week
            int DaysInFirstWeek = 8 - (StartWeekDayOfYear);

            // If the year either starts or ends on a thursday it will have a 53rd week
            if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear == THURSDAY)
                ThursdayFlag = true;

            // We begin by calculating the number of FULL weeks between the start of the year and
            // our date. The number is rounded up, so the smallest possible value is 0.
            var FullWeeks = (int) Math.Ceiling((DayOfYear - (DaysInFirstWeek))/7.0);

            int WeekNumber = FullWeeks;

            // If the first week of the year has at least four days, then the actual week number for our date
            // can be incremented by one.
            if (DaysInFirstWeek >= THURSDAY)
                WeekNumber = WeekNumber + 1;

            // If week number is larger than week 52 (and the year doesn't either start or end on a thursday)
            // then the correct week number is 1.
            if (WeekNumber > 52 && !ThursdayFlag)
                WeekNumber = 1;

            // If week number is still 0, it means that we are trying to evaluate the week number for a
            // week that belongs in the previous year (since that week has 3 days or less in our date's year).
            // We therefore make a recursive call using the last day of the previous year.
            if (WeekNumber == 0)
                WeekNumber = new DateTime(date.Year - 1, DEC, LASTDAYOFDEC).GetWeekNumber();
            return WeekNumber;
        }

        #endregion

    }
}

   
    
    
  








Related examples in the same category

1.Gets the days between.
2.Return a unique identifier based on system's full date (yyyymmdd) and time (hhmissms).
3.Return a elapsed time in formatted string. (hh:mm:ss:mi)
4.Screen for holidays
5.Return the previous business date of the date specified.
6.Return the previous or next business day of the date specified.
7.Return true if the number of seconds has elapsed since the last check
8.Add Business Days
9.Get Day Of Week
10.Add week to a DateTime
11.Get the quarter number for the DateTime
12.Date and time To Words
13.Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value.
14.Convert Unix Seconds
15.Get Elapsed Time
16.Is given DateTime Weekend
17.Converts a Date to a string using relative time.
18.Gets the date from Year integer
19.Gets the date from Year and Month integer
20.Gets the date from Year, Month and Day integer
21.Gets the days in month.
22.Gets the days in year.
23.Gets the end of day.
24.Gets the end of week.
25.Gets the start of month.
26.Gets the end of month.
27.Gets the end of quarter.
28.Gets the end of year.
29.Gets the months between.
30.Date Time To String
31.Update text within a file by replacing a substring within the file.
32.Get Date Difference in String
33.Converts the specified date and time strings to their DateTime equivalent representation.
34.Create Expires In String
35.Compares 2 dates ignoring the milliseconds
36.Calculate date, based on specified time and unit
37.Gets the ordinal suffix for a given date
38.Generate the timestamp of the provided DateTime
39.Converts the date to start from midnight.
40.Converts the date to end at midnight.
41.Convert time from seconds to ticks
42.Generate the UNIX style timestamp for DateTime.UtcNow
43.Parse DateTime from "2011/5/2 14:40"