TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DayOfWeek Enum

This C# article demonstrates the DayOfWeek enum type. It contains Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

DayOfWeek. Seven days are in each week.

We determine if a certain date is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday. The DayOfWeek property, and the DayOfWeek enum type, can be used for this purpose.

Example. To begin, we look at a program that acquires the current DayOfWeek. You can use either DateTime.Now or DateTime.Today to get the current day. And then you can take the DayOfWeek from the DateTime instance.

DateTime.Now

Next: We show how to test what day of the week was found using an if-expression.

And: We finally print out all the values of the DayOfWeek enum for completeness.

Console.WriteLine

Based on:

.NET 4

C# program that uses DayOfWeek enum

using System;

class Program
{
    static void Main()
    {
	// Get currrent day of week.
	DayOfWeek today = DateTime.Today.DayOfWeek;
	Console.WriteLine("Today is {0}",
	    today);

	// Test current day of week.
	if (today == DayOfWeek.Monday)
	{
	    Console.WriteLine("DO WORK");
	}

	// Demonstrate all DayOfWeek values.
	Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
	    DayOfWeek.Monday,
	    DayOfWeek.Tuesday,
	    DayOfWeek.Wednesday,
	    DayOfWeek.Thursday,
	    DayOfWeek.Friday,
	    DayOfWeek.Saturday,
	    DayOfWeek.Sunday);
    }
}

Output

Today is Monday
DO WORK
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

Selection statement on DayOfWeek. You can use an if-expression or a switch statement with the DayOfWeek enum. If you have to take different actions based on every day, you could use a switch statement and then execute code as required.

Note: In the example output, the DO WORK line only appears if the program was run on a Monday.

SwitchEnum

Summary. The DayOfWeek enum provides a handy encoded type for representing the day of the week as a small value. This type can be used anywhere. It is not tied to the DateTime type or the DayOfWeek property accessor.

Property


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf