Check if dateTime is a holiday or a business day
<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 check if the current date is a business day or a day off, then I would like to print a response to the user. I am currently getting Runtime Error. If I delete the if statement, the first elements (current date and day of the week) will be printed correctly.
You wrote the wrong varable in the following if statement:
if ((dayToday == DayOfWeek.Saturday) || (dayToday == DayOfWeek.Sunday)) { Console.WriteLine("This is a weekend"); } instead of dayToday you should use day varable in the condition.
UPDATE: You also made a mistake in the state. Must be or instead of and .
Correct code
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday)) { Console.WriteLine("This is a weekend"); } You are comparing the ASP.NET dayToday with the DayOfWeek enumeration element, which of course does not work
Perhaps you want to replace dayToday with day in your if , i.e. from
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday)) to
if ((day == DayOfWeek.Saturday) && (day == DayOfWeek.Sunday)) In addition, you probably also want to replace the logical "and" ( && ) with a logical "or" ( || ), finally
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday)) You get an error because you are comparing an enum with a string.
// dayToday is a string // DayOfWeek.Saturday is an enum if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday)) Use DayOfWeek.Saturday.ToString() to compare against a string. You also do not need to insert the dayToday string. Alternatively, use the day variable to compare with the enumeration.
https://dotnetfiddle.net/gUGJ0J
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(); // compare enums if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday)) { Console.WriteLine(dateToday + " is a weekend"); } else { Console.WriteLine(dateToday + " is not a weekend"); } // compare strings if ((dayToday == DayOfWeek.Saturday.ToString()) || (dayToday == DayOfWeek.Sunday.ToString())) { Console.WriteLine(dateToday + " is a weekend"); } else { Console.WriteLine(dateToday + " is not a weekend"); } } } You need to put your asp controls in the form tag using runat = "server".
<body> <form id="frm" runat="server"> <p> Today date is: <asp:Label ID="dateToday" runat="server" /> <br/> The day of the week is: <asp:Label ID="dayToday" runat="server" /> <br/> </form> </body> The signature of the Page_Load method is incorrect in your code. You should also use Response.Write for asp sites instead of Console.Writeline . When checking the day of the week, there should be OR (||) instead of AND (& &).
<script language="c#" runat="server"> protected void Page_Load(object sender, EventArgs e) { DateTime date = DateTime.Now; dateToday.Text = " " + date.ToString("d"); DayOfWeek day = DateTime.Now.DayOfWeek; dayToday.Text = " " + day.ToString(); if ((dayToday.Text == DayOfWeek.Saturday.ToString()) || (dayToday.Text == DayOfWeek.Sunday.ToString())) { Response.Write("This is a weekend"); } } </script> List<DateTime> datelist = new List<DateTime>(); int balanceday = 1; while (datelist.Count < 10) { DateTime day = DateTime.Now.AddDays(balanceday + datelist.Count).Date; if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday) { datelist.Add(day); } else { balanceday++; } }