| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Extract the Day of the Week from a Specific Date |
03/30/2017 |
.net |
dotnet-standard |
article |
|
1c9bef76-5634-46cf-b91c-9b9eb72091d7 |
12 |
rpetrusha |
ronpet |
wpickett |
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 xref:System.DateTime.DayOfWeek%2A or xref:System.DateTimeOffset.DayOfWeek%2A 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 xref:System.String.Format%2A?displayProperty=nameWithType method. This topic shows how to perform these formatting operations.
To extract a number indicating the day of the week from a specific date
-
If you are working with the string representation of a date, convert it to a xref:System.DateTime or a xref:System.DateTimeOffset value by using the static xref:System.DateTime.Parse%2A?displayProperty=nameWithType or xref:System.DateTimeOffset.Parse%2A?displayProperty=nameWithType method.
-
Use the xref:System.DateTime.DayOfWeek%2A?displayProperty=nameWithType or xref:System.DateTimeOffset.DayOfWeek%2A?displayProperty=nameWithType property to retrieve a xref:System.DayOfWeek value that indicates the day of the week.
-
If necessary, cast (in C#) or convert (in Visual Basic) the xref:System.DayOfWeek value to an integer.
The following example displays an integer that represents the day of the week of a specific date.
[!code-csharpFormatting.Howto.WeekdayName#7] [!code-vbFormatting.Howto.WeekdayName#7]
To extract the abbreviated weekday name from a specific date
-
If you are working with the string representation of a date, convert it to a xref:System.DateTime or a xref:System.DateTimeOffset value by using the static xref:System.DateTime.Parse%2A?displayProperty=nameWithType or xref:System.DateTimeOffset.Parse%2A?displayProperty=nameWithType method.
-
You can extract the abbreviated weekday name of the current culture or of a specific culture:
-
To extract the abbreviated weekday name for the current culture, call the date and time value's xref:System.DateTime.ToString%28System.String%29?displayProperty=nameWithType or xref:System.DateTimeOffset.ToString%28System.String%29?displayProperty=nameWithType instance method, and pass the string "ddd" as the
formatparameter. The following example illustrates the call to the xref:System.DateTime.ToString%28System.String%29 method.[!code-csharpFormatting.Howto.WeekdayName#1] [!code-vbFormatting.Howto.WeekdayName#1]
-
To extract the abbreviated weekday name for a specific culture, call the date and time value’s xref:System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType or xref:System.DateTimeOffset.ToString%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType instance method. Pass the string "ddd" as the
formatparameter. Pass either a xref:System.Globalization.CultureInfo or a xref:System.Globalization.DateTimeFormatInfo object that represents the culture whose weekday name you want to retrieve as theproviderparameter. The following code illustrates a call to the xref:System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 method using a xref:System.Globalization.CultureInfo object that represents the fr-FR culture.[!code-csharpFormatting.Howto.WeekdayName#2] [!code-vbFormatting.Howto.WeekdayName#2]
-
To extract the full weekday name from a specific date
-
If you are working with the string representation of a date, convert it to a xref:System.DateTime or a xref:System.DateTimeOffset value by using the static xref:System.DateTime.Parse%2A?displayProperty=nameWithType or xref:System.DateTimeOffset.Parse%2A?displayProperty=nameWithType method.
-
You can extract the full weekday name of the current culture or of a specific culture:
-
To extract the weekday name for the current culture, call the date and time value’s xref:System.DateTime.ToString%28System.String%29?displayProperty=nameWithType or xref:System.DateTimeOffset.ToString%28System.String%29?displayProperty=nameWithType instance method, and pass the string "dddd" as the
formatparameter. The following example illustrates the call to the xref:System.DateTime.ToString%28System.String%29 method.[!code-csharpFormatting.Howto.WeekdayName#4] [!code-vbFormatting.Howto.WeekdayName#4]
-
To extract the weekday name for a specific culture, call the date and time value’s xref:System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType or xref:System.DateTimeOffset.ToString%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType instance method. Pass the string "dddd" as the
formatparameter. Pass either a xref:System.Globalization.CultureInfo or a xref:System.Globalization.DateTimeFormatInfo object that represents the culture whose weekday name you want to retrieve as theproviderparameter. The following code illustrates a call to the xref:System.DateTime.ToString%28System.String%2CSystem.IFormatProvider%29 method using a xref:System.Globalization.CultureInfo object that represents the es-ES culture.[!code-csharpFormatting.Howto.WeekdayName#5] [!code-vbFormatting.Howto.WeekdayName#5]
-
Example
The example illustrates calls to the xref:System.DateTime.DayOfWeek%2A?displayProperty=nameWithType and xref:System.DateTimeOffset.DayOfWeek%2A?displayProperty=nameWithType properties and the xref:System.DateTime.ToString%2A?displayProperty=nameWithType and xref:System.DateTimeOffset.ToString%2A?displayProperty=nameWithType 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.
[!code-csharpFormatting.Howto.WeekdayName#6] [!code-vbFormatting.Howto.WeekdayName#6]
Individual languages may provide functionality that duplicates or supplements the functionality provided by the [!INCLUDEdnprdnshort]. 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 xref:System.DateTime.DayOfWeek%2A?displayProperty=nameWithType 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.
[!code-vbFormatting.HowTo.WeekdayName#9]
You can also use the value returned by the xref:System.DateTime.DayOfWeek%2A?displayProperty=nameWithType property to retrieve the weekday name of a particular date. This requires only a call to the xref:System.Enum.ToString%2A method on the xref:System.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.
[!code-csharpFormatting.HowTo.WeekdayName#8] [!code-vbFormatting.HowTo.WeekdayName#8]
See Also
Performing Formatting Operations
Standard Date and Time Format Strings
Custom Date and Time Format Strings