Share via


How to: Extract the Day of the Week from a Specific Date

The .NET Framework makes it easy to determine the ordinal day of the week for a particular date, and to display the localized weekday name for a particular date. An enumerated value that indicates the day of the week corresponding to a particular date is available from the DayOfWeek or DayOfWeek property. In contrast, retrieving the weekday name is a formatting operation that can be performed by calling a formatting method, such as a date and time value's ToString method or the String.Format method. This topic shows how to perform these formatting operations.

To extract a number indicating the day of the week from a specific date

  1. If you are working with the string representation of a date, convert it to a DateTime or a DateTimeOffset value by using the static DateTime.Parse or DateTimeOffset.Parse method.

  2. Use the DateTime.DayOfWeek or DateTimeOffset.DayOfWeek property to retrieve a DayOfWeek value that indicates the day of the week.

  3. If necessary, cast (in C#) or convert (in Visual Basic) the DayOfWeek value to an integer.

The following example displays an integer that represents the day of the week from the string representation of a date.

Dim dateValue As Date = #6/11/2008#
Console.WriteLine(dateValue.DayOfWeek)           ' Displays 3
DateTime dateValue = new DateTime(2008, 6, 11);
Console.WriteLine((int) dateValue.DayOfWeek);      // Displays 3

To extract the abbreviated weekday name from a specific date

  1. If you are working with the string representation of a date, convert it to a DateTime or a DateTimeOffset value by using the static DateTime.Parse or DateTimeOffset.Parse method.

  2. You can extract the abbreviated weekday name of the current culture or of a specific culture:

    1. To extract the abbreviated weekday name for the current culture, call the date and time value's DateTime.ToString(String) or DateTimeOffset.ToString(String) instance method, and pass the string "ddd" as the format parameter. The following example illustrates the call to the ToString(String) method.

      Dim dateValue As Date = #6/11/2008#
      Console.WriteLine(dateValue.ToString("ddd"))    ' Displays Wed
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine(dateValue.ToString("ddd"));    // Displays Wed
      
    2. To extract the abbreviated weekday name for a specific culture, call the date and time value’s DateTime.ToString(String, IFormatProvider) or DateTimeOffset.ToString(String, IFormatProvider) instance method. Pass the string "ddd" as the format parameter. Pass either a CultureInfo or a DateTimeFormatInfo object that represents the culture whose weekday name you want to retrieve as the provider parameter. The following code illustrates a call to the ToString(String, IFormatProvider) method using a CultureInfo object that represents the fr-FR culture.

      Dim dateValue As Date = #6/11/2008#
      Console.WriteLine(dateValue.ToString("ddd", _
                        New CultureInfo("fr-FR")))    ' Displays mer.
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine(dateValue.ToString("ddd", 
                        new CultureInfo("fr-FR")));    // Displays mer.
      

To extract the full weekday name from a specific date

  1. If you are working with the string representation of a date, convert it to a DateTime or a DateTimeOffset value by using the static DateTime.Parse or DateTimeOffset.Parse method.

  2. You can extract the full weekday name of the current culture or of a specific culture:

    1. To extract the weekday name for the current culture, call the date and time value’s DateTime.ToString(String) or DateTimeOffset.ToString(String) instance method, and pass the string "dddd" as the format parameter. The following example illustrates the call to the ToString(String) method.

      Dim dateValue As Date = #6/11/2008#
      Console.WriteLine(dateValue.ToString("dddd"))    ' Displays Wednesday
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine(dateValue.ToString("dddd"));    // Displays Wednesday
      
    2. To extract the weekday name for a specific culture, call the date and time value’s DateTime.ToString(String, IFormatProvider) or DateTimeOffset.ToString(String, IFormatProvider) instance method. Pass the string "dddd" as the format parameter. Pass either a CultureInfo or a DateTimeFormatInfo object that represents the culture whose weekday name you want to retrieve as the provider parameter. The following code illustrates a call to the ToString(String, IFormatProvider) method using a CultureInfo object that represents the es-ES culture.

      Dim dateValue As Date = #6/11/2008#
      Console.WriteLine(dateValue.ToString("dddd", _
                        New CultureInfo("es-ES")))     ' Displays miércoles.
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine(dateValue.ToString("dddd", 
                        new CultureInfo("es-ES")));    // Displays miércoles.
      

Example

The example illustrates calls to the DateTime.DayOfWeek and DateTimeOffset.DayOfWeek properties and the DateTime.ToString and DateTimeOffset.ToString methods to retrieve the number that represents the day of the week, the abbreviated weekday name, and the full weekday name for a particular date.

Dim dateString As String = "6/11/2007" 
Dim dateValue As Date 
Dim dateOffsetValue As DateTimeOffset

Try 
   Dim dateTimeFormats As DateTimeFormatInfo
   ' Convert date representation to a date value
   dateValue = Date.Parse(dateString, CultureInfo.InvariantCulture)
   dateOffsetValue = New DateTimeOffset(dateValue, _
                               TimeZoneInfo.Local.GetUtcOffset(dateValue))            
   ' Convert date representation to a number indicating the day of week
   Console.WriteLine(dateValue.DayOfWeek)
   Console.WriteLine(dateOffsetValue.DayOfWeek)

   ' Display abbreviated weekday name using current culture
   Console.WriteLine(dateValue.ToString("ddd"))
   Console.WriteLine(dateOffsetValue.ToString("ddd"))

   ' Display full weekday name using current culture
   Console.WriteLine(dateValue.ToString("dddd"))
   Console.WriteLine(dateOffsetValue.ToString("dddd"))

   ' Display abbreviated weekday name for de-DE culture
   Console.WriteLine(dateValue.ToString("ddd", New CultureInfo("de-DE")))
   Console.WriteLine(dateOffsetValue.ToString("ddd", _
                                              New CultureInfo("de-DE")))

   ' Display abbreviated weekday name with de-DE DateTimeFormatInfo object
   dateTimeFormats = New CultureInfo("de-DE").DateTimeFormat
   Console.WriteLine(dateValue.ToString("ddd", dateTimeFormats))
   Console.WriteLine(dateOffsetValue.ToString("ddd", dateTimeFormats))

   ' Display full weekday name for fr-FR culture
   Console.WriteLine(dateValue.ToString("ddd", New CultureInfo("fr-FR")))
   Console.WriteLine(dateOffsetValue.ToString("ddd", _
                                              New CultureInfo("fr-FR")))

   ' Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
   dateTimeFormats = New CultureInfo("fr-FR").DateTimeFormat
   Console.WriteLine(dateValue.ToString("dddd", dateTimeFormats))
   Console.WriteLine(dateOffsetValue.ToString("dddd", dateTimeFormats))
Catch e As FormatException
   Console.WriteLine("Unable to convert {0} to a date.", dateString)
End Try 
' The example displays the following output to the console: 
'       1 
'       1 
'       Mon 
'       Mon 
'       Monday 
'       Monday 
'       Mo 
'       Mo 
'       Mo 
'       Mo 
'       lun. 
'       lun. 
'       lundi 
'       lundi
string dateString = "6/11/2007";
DateTime dateValue;
DateTimeOffset dateOffsetValue;

try
{
   DateTimeFormatInfo dateTimeFormats;
   // Convert date representation to a date value
   dateValue = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
   dateOffsetValue = new DateTimeOffset(dateValue, 
                                TimeZoneInfo.Local.GetUtcOffset(dateValue));         

   // Convert date representation to a number indicating the day of week
   Console.WriteLine((int) dateValue.DayOfWeek);
   Console.WriteLine((int) dateOffsetValue.DayOfWeek);

   // Display abbreviated weekday name using current culture
   Console.WriteLine(dateValue.ToString("ddd"));
   Console.WriteLine(dateOffsetValue.ToString("ddd"));

   // Display full weekday name using current culture
   Console.WriteLine(dateValue.ToString("dddd"));
   Console.WriteLine(dateOffsetValue.ToString("dddd"));

   // Display abbreviated weekday name for de-DE culture
   Console.WriteLine(dateValue.ToString("ddd", new CultureInfo("de-DE")));
   Console.WriteLine(dateOffsetValue.ToString("ddd", 
                                               new CultureInfo("de-DE")));

   // Display abbreviated weekday name with de-DE DateTimeFormatInfo object
   dateTimeFormats = new CultureInfo("de-DE").DateTimeFormat;
   Console.WriteLine(dateValue.ToString("ddd", dateTimeFormats));
   Console.WriteLine(dateOffsetValue.ToString("ddd", dateTimeFormats));

   // Display full weekday name for fr-FR culture
   Console.WriteLine(dateValue.ToString("ddd", new CultureInfo("fr-FR")));
   Console.WriteLine(dateOffsetValue.ToString("ddd", 
                                              new CultureInfo("fr-FR")));

   // Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
   dateTimeFormats = new CultureInfo("fr-FR").DateTimeFormat;
   Console.WriteLine(dateValue.ToString("dddd", dateTimeFormats));
   Console.WriteLine(dateOffsetValue.ToString("dddd", dateTimeFormats));
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert {0} to a date.", dateString);
}
// The example displays the following output to the console: 
//       1 
//       1 
//       Mon 
//       Mon 
//       Monday 
//       Monday 
//       Mo 
//       Mo 
//       Mo 
//       Mo 
//       lun. 
//       lun. 
//       lundi 
//       lundi

Individual languages may provide functionality that duplicates or supplements the functionality provided by the .NET Framework. For example, Visual Basic includes two such functions:

  • Weekday, which returns a number that indicates the day of the week of a particular date. It considers the ordinal value of the first day of the week to be one, whereas the DateTime.DayOfWeek property considers it to be zero.

  • WeekdayName, which returns the name of the week in the current culture that corresponds to a particular weekday number.

The following example illustrates the use of the Visual Basic Weekday and WeekdayName functions.

Dim dateValue As Date = #6/11/2008#

' Get weekday number using Visual Basic Weekday function
Console.WriteLine(Weekday(dateValue))                 ' Displays 4
' Compare with .NET DateTime.DayOfWeek property
Console.WriteLine(dateValue.DayOfWeek)                ' Displays 3

' Get weekday name using Weekday and WeekdayName functions
Console.WriteLine(WeekdayName(Weekday(dateValue)))    ' Displays Wednesday

' Change culture to de-DE 
Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
' Get weekday name using Weekday and WeekdayName functions
Console.WriteLine(WeekdayName(Weekday(dateValue)))   ' Displays Donnerstag

' Restore original culture
Thread.CurrentThread.CurrentCulture = originalCulture   

You can also use the value returned by the DateTime.DayOfWeek property to retrieve the weekday name of a particular date. This requires only a call to the ToString method on the DayOfWeek value returned by the property. However, this technique does not produce a localized weekday name for the current culture, as the following example illustrates.

' Change current culture to fr-FR 
Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")

Dim dateValue As Date = #6/11/2008#
' Display the DayOfWeek string representation
Console.WriteLine(dateValue.DayOfWeek.ToString())     ' Displays Wednesday
' Restore original current culture
Thread.CurrentThread.CurrentCulture = originalCulture
// Change current culture to fr-FR
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

DateTime dateValue = new DateTime(2008, 6, 11);
// Display the DayOfWeek string representation
Console.WriteLine(dateValue.DayOfWeek.ToString());   // Displays Wednesday 
// Restore original current culture
Thread.CurrentThread.CurrentCulture = originalCulture;

Compiling the Code

These examples may require:

  • That the following namespaces be imported:

The examples also require:

  • That a reference to System.Core.dll be added to any project to which the code examples are added.

See Also

Concepts

Formatting How-to Topics

Date and Time Format Strings

Standard Date and Time Format Strings

Custom Date and Time Format Strings