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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » Date Time UtilScreenshots 
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 = (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 = (intMath.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.Gets the days in month.
3.Gets the days in year.
4.Gets the end of day.
5.Gets the start of month.
6.Gets the end of month.
7.Gets the end of quarter.
8.Gets the end of year.
9.Gets the months between.
10.Return a unique identifier based on system's full date (yyyymmdd) and time (hhmissms).
11.Return a elapsed time in formatted string. (hh:mm:ss:mi)
12.Screen for holidays
13.Return the previous business date of the date specified.
14.Return the previous or next business day of the date specified.
15.Return true if the number of seconds has elapsed since the last check
16.Add Business Days
17.Get Day Of Week
18.Add week to a DateTime
19.Get the quarter number for the DateTime
20.Date and time To Words
21.Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value.
22.Convert Unix Seconds
23.Get Elapsed Time
24.Is given DateTime Weekend
25.Converts a Date to a string using relative time.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.