Click here to Skip to main content
11,705,453 members (46,595 online)
Click here to Skip to main content

Tagged as

DateTime extension method to give Week number

, 8 Sep 2011 CPOL 3.9K 1
Rate this:
Please Sign up or sign in to vote.
Here another approach taken from my article Time Period Library for .NET[^], which returns addditional the year of the week.// ------------------------------------------------------------------------public enum YearWeekType{ Calendar, Iso8601,} // enum YearWeekType //...
Here another approach taken from my article Time Period Library for .NET[^], which returns addditional the year of the week.
// ------------------------------------------------------------------------
public enum YearWeekType
{
  Calendar,
  Iso8601,
} // enum YearWeekType
  
// ------------------------------------------------------------------------
public static class TimeTool
{
 
  // ----------------------------------------------------------------------
  public static void GetWeekOfYear( DateTime moment, YearWeekType yearWeekType,
    out int year, out int weekOfYear )
  {
    GetWeekOfYear( moment, Thread.CurrentThread.CurrentCulture, yearWeekType, out year, out weekOfYear );
  } // GetWeekOfYear

  // ----------------------------------------------------------------------
  public static void GetWeekOfYear( DateTime moment, CultureInfo culture, YearWeekType yearWeekType,
    out int year, out int weekOfYear )
  {
    GetWeekOfYear( moment, culture, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek, yearWeekType,
      out year, out weekOfYear );
  } // GetWeekOfYear

  // ----------------------------------------------------------------------
  public static void GetWeekOfYear( DateTime moment, CultureInfo culture,
    CalendarWeekRule weekRule, DayOfWeek firstDayOfWeek, YearWeekType yearWeekType, out int year, out int weekOfYear )
  {
    if ( culture == null )
    {
      throw new ArgumentNullException( "culture" );
    }
 
    if ( yearWeekType == YearWeekType.Iso8601 && weekRule == CalendarWeekRule.FirstFourDayWeek && firstDayOfWeek == DayOfWeek.Monday )
    {
      DayOfWeek day = culture.Calendar.GetDayOfWeek( moment );
      if ( day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday )
      {
        moment = moment.AddDays( 3 );
      }
    }
 
    weekOfYear = culture.Calendar.GetWeekOfYear( moment, weekRule, firstDayOfWeek );
    year = moment.Year;
    if ( weekOfYear >= 52 && moment.Month < 12 )
    {
      year--;
    }
  } // GetWeekOfYear

} // class TimeTool

License

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

Share

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

You may also be interested in...

Comments and Discussions

 
General+5 - Your time period lib is a seminal piece of work I would... Pin
Reiss8-Sep-11 4:55
memberReiss8-Sep-11 4:55 
GeneralRe: Many thanks :) Pin
Jani Giannoudis8-Sep-11 5:08
mvpJani Giannoudis8-Sep-11 5:08 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 8 Sep 2011
Article Copyright 2011 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid