Check if dateTime is a weekend or a weekday

夙愿已清 提交于 2021-02-17 15:15:36

问题


<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.


回答1:


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.

UPDATE: Also you made mistake in condition. There must be or instead of and.

Correct code is

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



回答2:


You are comparing your ASP.NET label dayToday against an enumeration element of DayOfWeek which of course fails

Probably you want to replace dayToday with day in your if statement, 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' (||) to finally

if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))



回答3:


if ((day >= DayOfWeek.Monday) && (day<= DayOfWeek.Friday))
{
    // action
}



回答4:


You are receiving 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 will also want not to pad the dayToday string. Alternatively, use the day variable to compare against an enum.

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");
        }
    }
}



回答5:


        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++;
            }
        }



回答6:


You need to put your asp controls inside form tag with runat="server".

<body>
    <form id="frm" runat="server">
    <p>
    Today's date is: <asp:Label ID="dateToday" runat="server" />
    <br/>
    The day of the week is: <asp:Label ID="dayToday" runat="server" />
    <br/>
    </form>
</body>

Signature of Page_Load method is incorrect in your code. Also you should use Response.Write for asp websites instead of Console.Writeline. There should be OR(||) instead of AND(&&) when you are checking for the day of the week.

 <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>


来源:https://stackoverflow.com/questions/39715947/check-if-datetime-is-a-weekend-or-a-weekday

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!