Getting the Day Name for a Date
When developing software that displays dates, you may want to show the name of a day such as Monday, Tuesday, etc. The names should be shown in the user's preferred language. This is simple to achieve using either of the methods described in this tip.
The DateTime structure includes a property named DayOfWeek. This property returns a value from an enumeration that is also called DayOfWeek. The returned value can be processed as an integer or can be converted to a string holding the English version of the day name.
string day = DateTime.Now.DayOfWeek.ToString();
Console.WriteLine(day); // Outputs "Thursday"
Applying a Language Using Culture Information
The DayOfWeek property is useful but only returns a day of the week in English. To show the name is a different language, a culture information class from the Globalization namespace is used. The CultureInfo class contains information relating to the current culture or a named locale's regional settings including date and time formatting details. One of the properties, DayNames, is an array of strings containing the names of days. To run the following example of this property, ensure that you add using System.Globalization; to the top of your code.
CultureInfo local = CultureInfo.CurrentCulture;
CultureInfo germany = CultureInfo.GetCultureInfo("de-DE");
int day = (int)DateTime.Now.DayOfWeek;
Getting a Day Name Using String Conversion
The final method for retrieving a day name for a DateTime is to simply convert the date value into a string using a format specifier. For the full name the specifier should be 'dddd'. Using 'ddd' instead returns a shortened version of the day name.
DateTime today = DateTime.Now;
Console.WriteLine(today.ToString("dddd")); // Outputs "Thursday"
Console.WriteLine(today.ToString("ddd")); // Outputs "Thu"
6 December 2007