DayOfWeek Enumeration (System)

Switch View :
ScriptFree
.NET Framework Class Library
DayOfWeek Enumeration

Specifies the day of the week.

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

Visual Basic
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration DayOfWeek
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum DayOfWeek
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum class DayOfWeek
F#
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type DayOfWeek
Members

Member name Description
Supported by the XNA Framework Supported by Portable Class Library Sunday Indicates Sunday.
Supported by the XNA Framework Supported by Portable Class Library Monday Indicates Monday.
Supported by the XNA Framework Supported by Portable Class Library Tuesday Indicates Tuesday.
Supported by the XNA Framework Supported by Portable Class Library Wednesday Indicates Wednesday.
Supported by the XNA Framework Supported by Portable Class Library Thursday Indicates Thursday.
Supported by the XNA Framework Supported by Portable Class Library Friday Indicates Friday.
Supported by the XNA Framework Supported by Portable Class Library Saturday Indicates Saturday.
Remarks

The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

This enumeration is useful when it is desirable to have a strongly typed specification of the day of the week. For example, this enumeration is the type of the property value for the DateTime.DayOfWeek property.

The members of the DayOfWeek enumeration are not localized. To return the localized name of the day of the week, call the DateTime.ToString(String) or the DateTime.ToString(String, IFormatProvider) method with either the "ddd" or "dddd" format strings. The former format string produces the abbreviated weekday name; the latter produces the full weekday name.

Examples

The following example demonstrates the DateTime.DayOfWeek property and the DayOfWeek enumeration.

Visual Basic

' This example demonstrates the DateTime.DayOfWeek property
Imports System

Class Sample
   Public Shared Sub Main()
      ' Assume the current culture is en-US.
      ' Create a DateTime for the first of May, 2003.
      Dim dt As New DateTime(2003, 5, 1)
      Console.WriteLine("Is Thursday the day of the week for {0:d}?: {1}", _
                         dt, dt.DayOfWeek = DayOfWeek.Thursday)
      Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Is Thursday the day of the week for 5/1/2003?: True
'The day of the week for 5/1/2003 is Thursday.
'


C#

// This example demonstrates the DateTime.DayOfWeek property
using System;

class Sample 
{
    public static void Main() 
    {
// Assume the current culture is en-US.
// Create a DateTime for the first of May, 2003.
    DateTime dt = new DateTime(2003, 5, 1);
    Console.WriteLine("Is Thursday the day of the week for {0:d}?: {1}", 
                       dt, dt.DayOfWeek == DayOfWeek.Thursday);
    Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek);
    }
}
/*
This example produces the following results:

Is Thursday the day of the week for 5/1/2003?: True
The day of the week for 5/1/2003 is Thursday.
*/


Visual C++

// This example demonstrates the DateTime.DayOfWeek property
using namespace System;
int main()
{

   // Assume the current culture is en-US.
   // Create a DateTime for the first of May, 2003.
   DateTime dt = DateTime(2003,5,1);
   Console::WriteLine(  "Is Thursday the day of the week for {0:d}?: {1}", dt, dt.DayOfWeek == DayOfWeek::Thursday );
   Console::WriteLine(  "The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek );
}

/*
This example produces the following results:

Is Thursday the day of the week for 5/1/2003?: True
The day of the week for 5/1/2003 is Thursday.
*/


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Community Content

R Petrusha - MSFT
Underlying int values

Is it safe to make assumptions about the underlying int values of the enum? As in, are they fixed in documentation or not?

The Underlying Values


Note that, with rare exceptions, we don't document the underlying values of enumeration members. Part of the value of enumerated constants is that they free developers from having to work with numeric values and instead allow them to work with more intuitive names.

That said, changing the underlying values of enumeration members is a major breaking change. Because the .NET Framework aims at maximizing compatibility from version to version going forward, and also because a good deal of customer code uses the underlying value of the DayOfWeek enumeration to retrieve localized day names from the DateTimeFormatInfo object, it is safe to assume that the underlying values of the DayOfWeek enumeration will never change.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation


R Petrusha - MSFT
Is DayOfWeek dependent on the regional settings?

I mean, should I be running on a German-based OS, will DayOfWeek return, instead of Saturday, Samstag? What about the order of the days (the first day of the week in Germany is Monday)

DayOfWeekEnumeration and Regional Settings


DayOfWeek is an enumeration. Both the names and the values of its members remain constant across cultures. The enumeration is intended for use in methods, such as the Calendar.GetWeekOfYear method, that specifies the first day of the week as a method argument. Enumerations are not dependent on regional settings.

If you are interested in the culture-sensitive names of the days of the week or in the first day of the week for a particular culture that uses a particular calendar, use a DateTimeFormatInfo object that reflects the settings for a particular culture. For example, to get the localized names of the week for the German (Germany) culture and to determine the first day of the week, you can use code similar to the following:

using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo german = new CultureInfo("de-DE");
DateTimeFormatInfo dtfi = german.DateTimeFormat;

Console.WriteLine("Days of the week for the {0} culture:",
german.Name);
for (int ctr = 0; ctr < dtfi.DayNames.Length; ctr++)
Console.WriteLine(" {0,-12}{1}", dtfi.DayNames[ctr],
dtfi.DayNames[ctr] == dtfi.DayNames[(int)dtfi.FirstDayOfWeek] ?
"(First Day of Week)" : "");
}
}


--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation


R Petrusha - MSFT
small error


int myDay6 = 6;
DayOfWeek dw6 = (DayOfWeek)myDay6;
Console.WriteLine(myDay6 + " " + dw6.ToString()); // will print "6 Saturday"
int myDay7 = 7;
DayOfWeek dw7 = (DayOfWeek)myDay7;
Console.WriteLine(myDay7 + " " + dw7.ToString()); // will print "7 7"

This Is Not an Error


The underlying values of the DayOfWeek enumeration range from 0 (for DayOfWeek.Sunday) to 6 (for DayOfWeek.Saturday). The value of dw7, therefore, does not correspond to the underlying value of a member of the DayOfWeek enumeration. In this case, the call to the ToString method will always return the string representation of a number, rather than a string containing the name of the enumeration member.

More generally, a major justification for the use of enumeration types is that they free developers from having to work with the underlying values of enumerations, which in most cases are symbolic numbers. Rather than casting from an integer to the enumeration type, a much better practice in this case is simply to assign the DayOfWeek variable an enumeration value rather than an integer value. For example:

      DayOfWeek myDay6 = DayOfWeek.Saturday;
Console.WriteLine("{0:D} {0:G}", myDay6);
DayOfWeek myDay7 = DayOfWeek.Sunday;
Console.WriteLine("{0:D} {0:G}", myDay7);


If you want to ensure that an enumeration variable is not assigned an out-of-range value, you can call the Enum.IsDefined method to validate an enumeration's underlying value.

--Ron Petrusha
CLR Developer Content Team
Microsoft Corporation