penmas penmas - 2 months ago 22
C# Question

Check if dateTime is a weekend or a weekday

<script Language="c#" runat="server">
void Page_Load()
{
DateTime date = DateTime.Now;
dateToday.Text = " " + date.ToString("d");
DayOfWeek day = DateTime.Now.DayOfWeek;
dayToday.Text = " " + day.ToString();

if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}

}
</script>




Using dateTime, I am trying to test whether or not the current date is a weekday or weekend, then I would like to print the response to the user. Currently I am receiving a Runtime Error. If I remove my if statement the first items (the current date, and the day of the week) print properly.

Answer

You wrote wrong varable in the following if statement:

if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
{
    Console.WriteLine("This is a weekend");
}

instead of dayToday you must use day varable in the condition