检查dateTime是周末还是工作日
本文关键字:工作日 周末 检查 dateTime | 更新日期: 2023-09-27 18:09:02
<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>
使用dateTime,我试图测试当前日期是工作日还是周末,然后我想打印响应给用户。目前我收到一个运行时错误。如果我删除我的If语句,第一项(当前日期和星期几)将正确打印。
你在下面的if语句中写错了变量:
if ((dayToday == DayOfWeek.Saturday) || (dayToday == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}
代替dayToday必须在条件中使用day变量。
更新:你在条件上也犯了错误。必须有or
而不是and
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}
你正在比较你的ASP。. NET标签dayToday
对DayOfWeek
的枚举元素,当然失败
可能你想用day
替换dayToday
在你的if
语句,即从
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
if ((day == DayOfWeek.Saturday) && (day == DayOfWeek.Sunday))
此外,您可能还希望将逻辑'and' (&&
)替换为逻辑'or' (||
)以最终
if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
if ((day >= DayOfWeek.Monday) && (day<= DayOfWeek.Friday))
{
// action
}
您收到一个错误,因为您正在比较枚举和字符串。
// dayToday is a string
// DayOfWeek.Saturday is an enum
if ((dayToday == DayOfWeek.Saturday) && (dayToday == DayOfWeek.Sunday))
使用DayOfWeek.Saturday.ToString()
与字符串进行比较。您也不希望填充dayToday
字符串。或者,使用day
变量与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();
// 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");
}
}
}
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++;
}
}
你需要用runat="server"把你的asp控件放在form标签内。
<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>
Page_Load
方法的签名在你的代码中不正确。此外,你应该使用Response.Write
的asp网站,而不是Console.Writeline
。当您检查星期几时,应该使用OR(||)而不是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>