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

.NET 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 article shows how to perform these formatting operations.

Extract a number indicating the day of the week

  1. Use the static DateTime.Parse or DateTimeOffset.Parse method to convert the string representation of a date to a DateTime or a DateTimeOffset value.

  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 of a specific date:

using System;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine((int) dateValue.DayOfWeek);
   }
}
// The example displays the following output:
//       3
Module Example
    Public Sub Main()
        Dim dateValue As Date = #6/11/2008#
        Console.WriteLine(dateValue.DayOfWeek)
    End Sub
End Module
' The example displays the following output:
'    3

Extract the abbreviated weekday name

  1. Use the static DateTime.Parse or DateTimeOffset.Parse method to convert the string representation of a date to a DateTime or a DateTimeOffset value.

  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:

      using System;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("ddd"));
         }
      }
      // The example displays the following output:
       //       Wed
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("ddd"))
          End Sub
      End Module
      ' The example displays the following output:
      '       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:

      using System;
      using System.Globalization;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("ddd",
                              new CultureInfo("fr-FR")));
         }
      }
      // The example displays the following output:
      //       mer.
      
      Imports System.Globalization
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("ddd",
                                New CultureInfo("fr-FR")))
          End Sub
      End Module
      ' The example displays the following output:
      '       mer.
      

Extract the full weekday name

  1. Use the static DateTime.Parse or DateTimeOffset.Parse method to convert the string representation of a date to a DateTime or a DateTimeOffset value.

  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:

      using System;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("dddd"));
         }
      }
      // The example displays the following output:
      //       Wednesday
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("dddd"))
          End Sub
      End Module
      ' The example displays the following output:
      '       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:

      using System;
      using System.Globalization;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("dddd",
                              new CultureInfo("es-ES")));
         }
      }
      // The example displays the following output:
      //       miƩrcoles.
      
      Imports System.Globalization
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("dddd", _
                                New CultureInfo("es-ES")))
          End Sub
      End Module
      ' The example displays the following output:
      '       miƩrcoles.
      

Example

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

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      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:
//       1
//       1
//       Mon
//       Mon
//       Monday
//       Monday
//       Mo
//       Mo
//       Mo
//       Mo
//       lun.
//       lun.
//       lundi
//       lundi
Imports System.Globalization

Module Example
    Public Sub Main()
        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
    End Sub
End Module
' 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 might provide functionality that duplicates or supplements the functionality provided by .NET. 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:

Imports System.Globalization
Imports System.Threading

Module Example
    Public Sub Main()
        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
    End Sub
End Module

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

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // 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());
      // Restore original current culture
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}
// The example displays the following output:
//       Wednesday
Imports System.Globalization
Imports System.Threading

Module Example
    Public Sub Main()
        ' 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())
        ' Restore original current culture
        Thread.CurrentThread.CurrentCulture = originalCulture
    End Sub
End Module
' The example displays the following output:
'       Wednesday

See also