检查dateTime是周末还是工作日 [英] Check if dateTime is a weekend or a weekday

查看:84
本文介绍了检查dateTime是周末还是工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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语句,则前几项(当前日期和星期几)可以正确打印.

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.

推荐答案

您在以下if语句中写了错误的varable:

You wrote wrong varable in the following if statement:

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

您必须在条件中使用 day 变量,而不是 dayToday .

instead of dayToday you must use day varable in the condition.

更新:您也犯了状况错误.必须存在或,而不是 and .

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

正确的代码是

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

这篇关于检查dateTime是周末还是工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆