Export (0) Print
Expand All

JapaneseCalendar.GetWeekOfYear Method

Returns the week of the year that includes the date in the specified DateTime.

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)] 
public override int GetWeekOfYear (
	DateTime time,
	CalendarWeekRule rule,
	DayOfWeek firstDayOfWeek
)
/** @attribute ComVisibleAttribute(false) */ 
public int GetWeekOfYear (
	DateTime time, 
	CalendarWeekRule rule, 
	DayOfWeek firstDayOfWeek
)
ComVisibleAttribute(false) 
public override function GetWeekOfYear (
	time : DateTime, 
	rule : CalendarWeekRule, 
	firstDayOfWeek : DayOfWeek
) : int
Not applicable.

Parameters

time

The DateTime to read.

rule

One of the CalendarWeekRule values that defines a calendar week.

firstDayOfWeek

One of the DayOfWeek values that represents the first day of the week.

Return Value

A 1-based integer that represents the week of the year that includes the date in the time parameter.

Exception typeCondition

ArgumentOutOfRangeException

time or firstDayOfWeek is outside the range supported by the calendar.

-or-

rule is not a valid CalendarWeekRule value.

This method can be used to determine the number of weeks in the year by setting the time parameter to the last day of the year.

The CultureInfo.DateTimeFormat property contains culture-specific values that can be used for the rule and firstDayOfWeek parameters.

The FirstDayOfWeek property of CultureInfo.DateTimeFormat contains the default DayOfWeek value that represents the first day of the week for a specific culture, using the calendar specified in the Calendar property of CultureInfo.DateTimeFormat.

The CalendarWeekRule property of CultureInfo.DateTimeFormat contains the default CalendarWeekRule value that defines a calendar week for a specific culture, using the calendar specified in the Calendar property of CultureInfo.DateTimeFormat.

For example, in GregorianCalendar, the GetWeekOfYear method for January 1 returns 1.

The following code example shows how the result of GetWeekOfYear varies depending on the FirstDayOfWeek and CalendarWeekRule values used. If the specified date is the last day of the year, GetWeekOfYear returns the total number of weeks in that year.

using System;
using System.Globalization;


public class SamplesCalendar  {

   public static void Main()  {

      // Gets the Calendar instance associated with a CultureInfo.
      CultureInfo myCI = new CultureInfo("en-US");
      Calendar myCal = myCI.Calendar;

      // Gets the DTFI properties required by GetWeekOfYear.
      CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
      DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

      // Displays the number of the current week relative to the beginning of the year.
      Console.WriteLine( "The CalendarWeekRule used for the en-US culture is {0}.", myCWR );
      Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW );
      Console.WriteLine( "Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW ));

      // Displays the total number of weeks in the current year.
      DateTime LastDay = new System.DateTime( DateTime.Now.Year, 12, 31 );
      Console.WriteLine( "There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear( LastDay, myCWR, myFirstDOW ), LastDay.Year );

   }

}

/*
This code produces the following output.  Results vary depending on the system date.

The CalendarWeekRule used for the en-US culture is FirstDay.
The FirstDayOfWeek used for the en-US culture is Sunday.
Therefore, the current week is Week 1 of the current year.
There are 53 weeks in the current year (2001).

*/

import System.* ;
import System.Globalization.* ;

public class SamplesCalendar
{
    public static void main(String[] args)
    {
        // Gets the Calendar instance associated with a CultureInfo.
        CultureInfo myCI =  new CultureInfo("en-US");
        Calendar myCal = myCI.get_Calendar();

        // Gets the DTFI properties required by GetWeekOfYear.
        CalendarWeekRule myCWR = 
            myCI.get_DateTimeFormat().get_CalendarWeekRule();
        DayOfWeek myFirstDOW = myCI.get_DateTimeFormat().get_FirstDayOfWeek();

        // Displays the number of the current week relative to the 
        // beginning of the year.
        Console.WriteLine("The CalendarWeekRule used for the en-US culture " 
            + "is {0}.", myCWR);
        Console.WriteLine("The FirstDayOfWeek used for the en-US culture " 
            + "is {0}.", myFirstDOW);
        Console.WriteLine("Therefore, the current week is Week {0} of the " 
            + "current year.", System.Convert.ToString( myCal.GetWeekOfYear(
            DateTime.get_Now(),myCWR, myFirstDOW)));

        // Displays the total number of weeks in the current year.
        DateTime LastDay = 
            new System.DateTime(DateTime.get_Now().get_Year(), 12, 31);
        Console.WriteLine("There are {0} weeks in the current year ({1}).",
            System.Convert.ToString(myCal.GetWeekOfYear(LastDay, myCWR, 
            myFirstDOW)),System.Convert.ToString( LastDay.get_Year()));
    } //main 
} //SamplesCalendar

/*
This code produces the following output.  Results vary depending on the 
system date.

The CalendarWeekRule used for the en-US culture is FirstDay.
The FirstDayOfWeek used for the en-US culture is Sunday.
Therefore, the current week is Week 15 of the current year.
There are 53 weeks in the current year (2004).
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft