Updated on

To check if a DateTime falls on a weekend in C#, we read its DayOfWeek property and compare it with DayOfWeek.Saturday and DayOfWeek.Sunday. Any other day is a weekday.

Most programming languages work very well with numeric or textual data, but not all of them have good tools for dates and times. Luckily for us, C# has the built-in DateTime type, and the day of the week comes with it.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

What Is DayOfWeek in C#?

DayOfWeek is an enum in the System namespace with one member for each day of the week. Every DateTime has a DayOfWeek property that returns one of those members, so DateTime.Now.DayOfWeek tells us what day it is today.

The members have integer values too, and the enum starts the week on Sunday. Sunday is 0, Monday is 1, and so on up to Saturday, which is 6. Microsoft Learn says it directly: “If cast to an integer, its value ranges from zero (which indicates Sunday) to six (which indicates Saturday).”

DateTimeOffset and DateOnly have the same property, and it returns the same enum.

The member names are always English. DayOfWeek.ToString() returns “Sunday” whatever culture our app runs under. When we want the day name in the user’s language, we format the date with the “dddd” format string.

DateTime itself is a value type, and we use it for everything that involves dates, times and calculations with them. A DateTime value is a snapshot of a moment in time. It doesn’t store a time zone, so when the offset from UTC matters, DateTimeOffset is the type that carries it.

How Do We Compare DayOfWeek With Saturday and Sunday?

By using the DateTime type, we automatically get access to the property called DayOfWeek. Its value is always one of the seven days of the week.

There is no need to redefine the seven days. .NET comes with an enum for them, with a suggestive name: DayOfWeek.

So let’s see how to check if a certain day is a weekend day or a weekday. We’ll start with the current date:

var date = DateTime.Now;
if (date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday)
{
    Console.WriteLine("Weekend");
}
else
{
    Console.WriteLine("Workday");
}

Upon running this code, the message will be different depending on the day we run it. That also makes the code hard to test, since the result changes every day. For code that reads the current time, we can inject a TimeProvider instead of calling DateTime.Now, as we show in our article on testing time-dependent code with TimeProvider.

And we can apply the same check for any other day:

date = new DateTime(2021, 10, 10);
if (date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday)
{
    Console.WriteLine("Weekend");
}
else
{
    Console.WriteLine("Workday");
}

This message is constant. It shows whether October 10, 2021 was a workday or a weekend day. A quick check of the calendar says it’s a Sunday, so in our case, it is a weekend day.

Writing the same if statement every time we need the check gets old quickly, so let’s move it into an extension method in a new DateTimeExtensions.cs file:

public static class DateTimeExtensions
{
    public static bool IsWeekend(this DateTime date) =>
        date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday;
}

Now the check is one call:

Console.WriteLine(new DateTime(2021, 10, 10).IsWeekend()); // True
Console.WriteLine(new DateTime(2021, 10, 8).IsWeekend());  // False

October 8, 2021 was a Friday, so it’s a workday for us.

What If the Weekend Isn’t Saturday and Sunday?

.NET has no built-in answer to that question. DayOfWeek names the days, and CultureInfo knows the local day names and which day starts the week, but nothing in the framework says which days are the weekend.

So the weekend is our decision, and the cleanest way to express it is to pass it to the check. Instead of hardcoding Saturday and Sunday, the method takes a set of weekend days and asks whether the date’s DayOfWeek is in it. The days can then come from configuration or from a per-user setting.

Weekend days are also only part of the question “is this a working day?”. A public holiday can fall on a Wednesday, and no DayOfWeek check will catch it. For that, we need a holiday calendar for each country we support, and .NET doesn’t ship one.

As you can see, there is no rigid rule about what the “weekend” means. We write the check for what defines a weekend day, in our case Saturday and Sunday. Some cultures consider the weekend to be Friday and Saturday, so let’s add an overload to DateTimeExtensions that takes the weekend days as a parameter:

public static bool IsWeekend(this DateTime date, IReadOnlySet<DayOfWeek> weekendDays) =>
    weekendDays.Contains(date.DayOfWeek);

And we call it with the days we need:

HashSet<DayOfWeek> fridaySaturday = [DayOfWeek.Friday, DayOfWeek.Saturday];

Console.WriteLine(new DateTime(2021, 10, 10).IsWeekend(fridaySaturday)); // False
Console.WriteLine(new DateTime(2021, 10, 8).IsWeekend(fridaySaturday));  // True

With a Friday and Saturday weekend, Sunday, October 10, 2021 is a workday, and Friday, October 8 is not.

These rules can change over time, too. If our app hardcodes the days, a change means a new build. If it reads them from configuration, we only change a setting.

Depending on culture, we can either choose one of the two weekends or support both if our scenarios require that. And when we need to count working days across a whole date range, we have a separate article on the number of weekdays between two dates.

Now that we have a nice mechanism to check this, let’s format it a bit.

How Do We Show the Day Name With the Result?

When displaying dates, there are many formats available, and our article on DateTime format strings covers them in detail. Here we only need the day name, and there are two ways to get it. The second one uses CultureInfo, so we add using System.Globalization; at the top of Program.cs:

var sunday = new DateTime(2021, 10, 10);
Console.WriteLine(sunday.DayOfWeek);                                   // Sunday
Console.WriteLine(sunday.ToString("dddd", new CultureInfo("de-DE")));  // Sonntag

The enum value prints its English name in every culture. The “dddd” format string gives us the full day name in the culture we pass, German in this case.

Now let’s modify our example to display a more detailed message. Besides a single word output, let’s show the date we are doing the checks on in a more readable format:

date = DateTime.Now;
Console.WriteLine($"The date {date:dddd, dd MMMM yyyy} is a {(date.IsWeekend() ? "weekend day" : "workday")}");

date = new DateTime(2021, 10, 10);
Console.WriteLine($"The date {date:dddd, dd MMMM yyyy} is a {(date.IsWeekend() ? "weekend day" : "workday")}");

Running it with the invariant culture on November 1, 2026, a Sunday, gives us:

The date Sunday, 01 November 2026 is a weekend day
The date Sunday, 10 October 2021 is a weekend day

The first line depends on the day we run the code, and the day and month names follow the current culture.

Okay, let’s recap what we’ve learned.

Conclusion

DateTime is a very useful data type for working with dates and times, and its DayOfWeek property is all we need to tell a weekend from a weekday. We’ve seen that DayOfWeek starts the week on Sunday at 0, that its names are never localized, and that the check fits nicely in an IsWeekend() extension method.

.NET doesn’t know which days are the weekend in a given country, so when our users don’t all share Saturday and Sunday, we pass the weekend days in.

Tested with .NET 10.0.10 and MSTest 4.5.2.