Share via


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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#
outputBlock.Text &= dateValue.DayOfWeek & vbCrLf           ' Displays 3
DateTime dateValue = new DateTime(2008, 6, 11);
outputBlock.Text += (int)dateValue.DayOfWeek + "\n";      // 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#
      outputBlock.Text &= dateValue.ToString("ddd") & vbCrLf    ' Displays Wed
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      outputBlock.Text += dateValue.ToString("ddd") + "\n";    // 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#
      outputBlock.Text += dateValue.ToString("ddd", _
                        New CultureInfo("fr-FR")) + vbCrLf    ' Displays mer. 
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      outputBlock.Text += dateValue.ToString("ddd",
                        new CultureInfo("fr-FR")) + "\n";    // 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#
      outputBlock.Text &= dateValue.ToString("dddd") & vbCrLf    ' Displays Wednesday
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      outputBlock.Text += dateValue.ToString("dddd") + "\n";    // 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#
      outputBlock.Text += dateValue.ToString("dddd", _
                        New CultureInfo("es-ES")) + vbCrLf       ' Displays miércoles
      
      DateTime dateValue = new DateTime(2008, 6, 11);
      outputBlock.Text += dateValue.ToString("dddd",
                        new CultureInfo("es-ES")) + "\n";    // 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
   outputBlock.Text &= dateValue.DayOfWeek & vbCrLf
   outputBlock.Text &= dateOffsetValue.DayOfWeek & vbCrLf

   ' Display abbreviated weekday name using current culture
   outputBlock.Text &= dateValue.ToString("ddd") & vbCrLf
   outputBlock.Text &= dateOffsetValue.ToString("ddd") & vbCrLf

   ' Display full weekday name using current culture
   outputBlock.Text &= dateValue.ToString("dddd") & vbCrLf
   outputBlock.Text &= dateOffsetValue.ToString("dddd") & vbCrLf

   ' Display abbreviated weekday name for de-DE culture
   outputBlock.Text &= dateValue.ToString("ddd", New CultureInfo("de-DE")) & vbCrLf
   outputBlock.Text += dateOffsetValue.ToString("ddd", _
                                              New CultureInfo("de-DE")) & vbCrLf

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

   ' Display full weekday name for fr-FR culture
   outputBlock.Text &= dateValue.ToString("ddd", New CultureInfo("fr-FR")) & vbCrLf
   outputBlock.Text += dateOffsetValue.ToString("ddd", _
                                              New CultureInfo("fr-FR")) & vbCrLf

   ' Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
   dateTimeFormats = New CultureInfo("fr-FR").DateTimeFormat
   outputBlock.Text &= dateValue.ToString("dddd", dateTimeFormats) & vbCrLf
   outputBlock.Text &= dateOffsetValue.ToString("dddd", dateTimeFormats) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("Unable to convert {0} to a date.", dateString) & vbCrLf
End Try
' The example displays the following output:
'       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
   outputBlock.Text += (int)dateValue.DayOfWeek + "\n";
   outputBlock.Text += (int)dateOffsetValue.DayOfWeek + "\n";

   // Display abbreviated weekday name using current culture
   outputBlock.Text += dateValue.ToString("ddd") + "\n";
   outputBlock.Text += dateOffsetValue.ToString("ddd") + "\n";

   // Display full weekday name using current culture
   outputBlock.Text += dateValue.ToString("dddd") + "\n";
   outputBlock.Text += dateOffsetValue.ToString("dddd") + "\n";

   // Display abbreviated weekday name for de-DE culture
   outputBlock.Text += dateValue.ToString("ddd", new CultureInfo("de-DE")) + "\n";
   outputBlock.Text += dateOffsetValue.ToString("ddd",
                                               new CultureInfo("de-DE")) + "\n";

   // Display abbreviated weekday name with de-DE DateTimeFormatInfo object
   dateTimeFormats = new CultureInfo("de-DE").DateTimeFormat;
   outputBlock.Text += dateValue.ToString("ddd", dateTimeFormats) + "\n";
   outputBlock.Text += dateOffsetValue.ToString("ddd", dateTimeFormats) + "\n";

   // Display full weekday name for fr-FR culture
   outputBlock.Text += dateValue.ToString("ddd", new CultureInfo("fr-FR")) + "\n";
   outputBlock.Text += dateOffsetValue.ToString("ddd",
                                              new CultureInfo("fr-FR")) + "\n";

   // Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
   dateTimeFormats = new CultureInfo("fr-FR").DateTimeFormat;
   outputBlock.Text += dateValue.ToString("dddd", dateTimeFormats) + "\n";
   outputBlock.Text += dateOffsetValue.ToString("dddd", dateTimeFormats) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("Unable to convert {0} to a date.", dateString) + "\n";
}
// The example displays the following output:
//       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.

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.

Compiling the Code

These examples may require:

  • That the following namespaces be imported: