C#idiomatic C#
how-to-extract-the-day-of-the-week-from-a-specific-datestandard/base-types

Core library

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

Learn how to determine the ordinal day of the week for a particular date in .NET. Learn how to display the localized weekday name for a particular date.

  • page standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date
  • updated 08/09/2022

.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 System.DateTime.DayOfWeek or System.DateTimeOffset.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 System.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 System.DateTime.Parse or System.DateTimeOffset.Parse method to convert the string representation of a date to a System.DateTime or a System.DateTimeOffset value.

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

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

The following example displays an integer that represents the day of the week of a specific date:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/weekdaynumber7.cs7

csharp
// <Snippet7>
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
// </Snippet7>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/weekdaynumber7.vb7

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet7>
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
' </Snippet7>

Extract the abbreviated weekday name

  1. Use the static System.DateTime.Parse or System.DateTimeOffset.Parse method to convert the string representation of a date to a System.DateTime or a System.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 System.DateTime.ToString%28System.String%29 or System.DateTimeOffset.ToString%28System.String%29 instance method, and pass the string ddd as the format parameter. The following example illustrates the call to the System.DateTime.ToString%28System.String%29 method:

      sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/abbrname1.cs1

      csharp
      // <Snippet1>
      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
      // </Snippet1>

      sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/abbrname1.vb1

      vb
      ' Visual Basic .NET Document
      Option Strict On
      
      ' <Snippet1>
      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
      ' </Snippet1>

    2. To extract the abbreviated weekday name for a specific culture, call the date and time value's System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 or System.DateTimeOffset.ToString%28System.String%2CSystem.IFormatProvider%29 instance method. Pass the string ddd as the format parameter. Pass either a System.Globalization.CultureInfo or a System.Globalization.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 System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 method using a System.Globalization.CultureInfo object that represents the fr-FR culture:

      sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/abbrname2.cs2

      csharp
      // <Snippet2>
      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.
      // </Snippet2>

      sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/abbrname2.vb2

      vb
      ' Visual Basic .NET Document
      Option Strict On
      
      ' <Snippet2>
      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.
      ' </Snippet2>

Extract the full weekday name

  1. Use the static System.DateTime.Parse or System.DateTimeOffset.Parse method to convert the string representation of a date to a System.DateTime or a System.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 System.DateTime.ToString%28System.String%29 or System.DateTimeOffset.ToString%28System.String%29 instance method, and pass the string dddd as the format parameter. The following example illustrates the call to the System.DateTime.ToString%28System.String%29 method:

      sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/fullname4.cs4

      csharp
      // <Snippet4>
      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
      // </Snippet4>

      sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/fullname4.vb4

      vb
      ' Visual Basic .NET Document
      Option Strict On
      
      ' <Snippet4>
      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
      ' </Snippet4>

    2. To extract the weekday name for a specific culture, call the date and time value's System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 or System.DateTimeOffset.ToString%28System.String%2CSystem.IFormatProvider%29 instance method. Pass the string dddd as the format parameter. Pass either a System.Globalization.CultureInfo or a System.Globalization.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 System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 method using a System.Globalization.CultureInfo object that represents the es-ES culture:

      sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/fullname5.cs5

      csharp
      // <Snippet5>
      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.
      // </Snippet5>

      sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/fullname5.vb5

      vb
      ' Visual Basic .NET Document
      Option Strict On
      
      ' <Snippet5>
      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.
      ' </Snippet5>

Example

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

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/example6.cs6

csharp
// <Snippet6>
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 {dateString} to a date.");
      }
   }
}
// The example displays the following output:
//       1
//       1
//       Mon
//       Mon
//       Monday
//       Monday
//       Mo
//       Mo
//       Mo
//       Mo
//       lun.
//       lun.
//       lundi
//       lundi
// </Snippet6>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/example6.vb6

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet6>
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
' </Snippet6>

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 System.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:

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/example9.vb9

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet9>
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
' </Snippet9>

You can also use the value returned by the System.DateTime.DayOfWeek property to retrieve the weekday name of a particular date. This process requires only a call to the System.Enum.ToString method on the System.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:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/cs/Howto1.cs8

csharp
// <Snippet8>
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
// </Snippet8>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.HowTo.WeekdayName/vb/Howto1.vb8

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet8>
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
' </Snippet8>

Public Class Example2
    Private Sub ShowAbbreviatedWithDateTimeInfo()
        ' <Snippet3>
        Dim dateValue As Date = #6/11/2008#
        Dim dateFormats As DateTimeFormatInfo = _
                           New CultureInfo("es-ES").DateTimeFormat
        Console.WriteLine(dateValue.ToString("ddd", _
                          dateFormats))    ' Displays mer.
        ' </Snippet3>
    End Sub

End Class

See also