Check if dateTime is a weekend or a weekday in c#

Check if dateTime is a weekend or a weekday in c#

In this post, use DayOfWeek.Saturday.ToString() to compare against a string. Alternatively, use the day variable to compare against an enum.

using System;

public class Program
{
    public static void Main()
    {
        DateTime date = DateTime.Now;
        string dateToday = date.ToString("d");
        DayOfWeek day = DateTime.Now.DayOfWeek;
        string dayToday = day.ToString();      
        if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
        {
            Console.WriteLine(dateToday + " is a weekend");
        }
        else
        {
            Console.WriteLine(dateToday + " is not a weekend");
        }
        if ((dayToday == DayOfWeek.Saturday.ToString()) || (dayToday == DayOfWeek.Sunday.ToString()))
        {
            Console.WriteLine(dateToday + " is a weekend");
        }
        else
        {
            Console.WriteLine(dateToday + " is not a weekend");
        }
    }
}

2020-02-18