Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to get current day of week
Use the DateTime.DayOfWeek property to get the current day of the week in C#. This property returns a DayOfWeek enumeration value representing the day.
Syntax
Following is the syntax to get the current day of the week −
DayOfWeek dayOfWeek = DateTime.Today.DayOfWeek;
You can also use DateTime.Now.DayOfWeek to include the current time −
DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;
Using DateTime.Today.DayOfWeek
The DateTime.Today property gets the current date with the time component set to 00:00:00. Using DayOfWeek on this returns the current day as an enumeration value −
using System;
public class Demo {
public static void Main() {
DayOfWeek wk = DateTime.Today.DayOfWeek;
Console.WriteLine("Current day of the week: " + wk);
Console.WriteLine("Day as integer: " + (int)wk);
}
}
The output of the above code is −
Current day of the week: Wednesday Day as integer: 3
Using DateTime.Now.DayOfWeek
You can also use DateTime.Now which includes the current time. The result for the day of week remains the same −
using System;
public class Demo {
public static void Main() {
DayOfWeek currentDay = DateTime.Now.DayOfWeek;
Console.WriteLine("Today is: " + currentDay);
// Display full date and time for reference
Console.WriteLine("Full date and time: " + DateTime.Now);
}
}
The output of the above code is −
Today is: Wednesday Full date and time: 12/13/2023 2:30:45 PM
DayOfWeek Enumeration Values
The DayOfWeek enumeration contains seven values, each representing a day of the week. Sunday is 0, Monday is 1, and so on −
| Day | Enumeration Value | Integer Value |
|---|---|---|
| Sunday | DayOfWeek.Sunday | 0 |
| Monday | DayOfWeek.Monday | 1 |
| Tuesday | DayOfWeek.Tuesday | 2 |
| Wednesday | DayOfWeek.Wednesday | 3 |
| Thursday | DayOfWeek.Thursday | 4 |
| Friday | DayOfWeek.Friday | 5 |
| Saturday | DayOfWeek.Saturday | 6 |
Using Switch Statement with DayOfWeek
You can use a switch statement to perform different actions based on the current day −
using System;
public class Demo {
public static void Main() {
DayOfWeek today = DateTime.Today.DayOfWeek;
switch (today) {
case DayOfWeek.Monday:
Console.WriteLine("Start of the work week!");
break;
case DayOfWeek.Wednesday:
Console.WriteLine("Midweek day!");
break;
case DayOfWeek.Friday:
Console.WriteLine("TGIF - Thank God It's Friday!");
break;
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
Console.WriteLine("Weekend time!");
break;
default:
Console.WriteLine("It's " + today);
break;
}
}
}
The output of the above code is −
Midweek day!
Conclusion
The DateTime.DayOfWeek property provides a simple way to get the current day of the week as a DayOfWeek enumeration. You can use either DateTime.Today.DayOfWeek or DateTime.Now.DayOfWeek depending on whether you need just the date or the current date and time.
