In this article, we are going to find out how to check if a DateTime is a Weekend or a Weekday.

Most programming languages work very well with numeric or textual data. But not all of them have the tools for handling information in the form of dates and times.  Luckily for us, with C#, we have the built-in type named DateTime

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

Let’s start.

What Is DateTime?

DateTime is a value type. We can use DateTimes in all our operations that require handling dates, times, and calculations with them. DateTimes can include timezone information as well. So we can develop globally available applications that are aware of timezone differences.

The DateTime type is available to C# applications through the System namespace.

A DateTime value is a snapshot of a moment in time. DateTimes come with many useful properties and operations available, making our work with dates easier. Let’s see how to put the DayOfWeek property to good use.

Check if a DateTime Is Weekend or Weekday

By using the DateTime type, we automatically get access to the property called DayOfWeek. The value for DayOfWeek will always be one of the seven days in a week, from Monday to Sunday.

There is no need to redefine the seven days, the C# package comes with an enum for them, with a suggestive name – DayOfWeek. We can go to its definition and see how the values are stored, for future references on how to do equality checks or comparisons.

NoteThe days in enum also have integer values assigned, from 0 to 6. Mind that Sunday = 0, Monday = 1, and so on until Saturday = 6. So in this representation, the week starts with Sunday.

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

var date = DateTime.Now;
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == 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.

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

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

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

As you can see, there was no rigid rule about what the “weekend” means, we explicitly write the checks for what defines a weekend day – in our case, Saturday and Sunday. And some cultures consider the weekend Friday-Saturday, so in this case, we can adapt our example:

var date = new DateTime(2021, 10, 10);
if (date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday)
{
    Console.WriteLine("Weekend");
}
else
{
    Console.WriteLine("Workday");
}

Depending on culture, we can either choose one of them or support both if our scenarios require that.

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

How to Format DateTime

When displaying dates, there are many formats available. The simple call of ToString() on a DateTime object might bring a default format. But we might prefer other formats, so let’s show a few common options, along with their results in a comment on the side:

Console.WriteLine(DateTime.Now.ToString());                              //Will show as: 12/8/2021 5:30:58 PM
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy"));                  //Will show as: 12/08/2021
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));          //Will show as: Wednesday, 08 December 2021
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")); //Will show as: Wednesday, 08 December 2021 17:35:32
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy HH:mm"));            //Will show as: 12/08/2021 17:35
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"));         //Will show as: 12/08/2021 05:35 PM
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy H:mm"));             //Will show as: 12/08/2021 17:35
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));          //Will show as: 12/08/2021 5:35 PM
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));         //Will show as: 12/08/2021 17:35:32

Now that we’ve seen what we have available, 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:

var date = DateTime.Now; 
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) 
{
    Console.WriteLine("The date " + date.ToString("dddd, dd MMMM yyyy") + " is a Weekend day"); 
} 
else 
{ 
    Console.WriteLine("The date " + date.ToString("dddd, dd MMMM yyyy") + " is a Workday"); 
} 
date = new DateTime(2021, 10, 10); 
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) 
{ 
    Console.WriteLine("The date " + date.ToString("dddd, dd MMMM yyyy") + " is a Weekend day"); 
} 
else 
{ 
    Console.WriteLine("The date " + date.ToString("dddd, dd MMMM yyyy") + " is a Workday"); 
} 

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

Conclusion

DateTime is a very useful data type when it comes to working with dates and times. In this article, we’ve learned what DateTime has to offer and more specifically about the DayOfWeek property.

We can use the DayOfWeek property to check if a DateTime is a Weekend or Weekday. We’ve learned how to use this property to decide whether a certain day is a weekday or a weekend day. Finally, we’ve learned about a few different options on how to format DateTimes to the screen.