-1

我试着在文本框中写 Even Weekend 或 Wekend,但什么也没有出现。

DateTime thisDate1 = DateTime.Now;
int day = thisDate1.DayOfYear;
int week = day / 7;
bool even = true;
if ((week % 2) == 0) even = true;
if ((week % 2) == 1) even = false;

if (even == true)
{
    if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Even Weekend";
    if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Even Weekend";
}
if (even == false)
{
    if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Weekend";
    if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Weekend";
}

非常感谢您的每一个回答。

4

2 回答 2

4

我假设你今天在星期天测试它。但是DayOfWeek.Sunday0,不是7

答案是不要转换为 int:

//if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Even Weekend";
//if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Even Weekend";
  if (thisDate1.DayOfWeek == DayofWeek.Saturday)  textBox1.Text = "Even Weekend";
  if (thisDate1.DayOfWeek == DayofWeek.Sunday)    textBox1.Text = "Even Weekend";
于 2013-11-10T19:51:01.390 回答
0

当 Ints 提供 DayOfWeek 枚举类型时,您为什么要使用它们来检查日期 http://msdn.microsoft.com/en-us/library/system.dayofweek%28v=vs.110%29.aspx

if (even == true)
            {
                if (thisDate1.DayOfWeek == DayOfWeek.Sunday) textBox1.Text = "Even Weekend";
                if (thisDate1.DayOfWeek == DayOfWeek.Saturday) textBox1.Text = "Even Weekend";


        }
    if (even == false)
    {
        if (thisDate1.DayOfWeek == DayOfWeek.Sunday) textBox1.Text = "Weekend";
        if (thisDate1.DayOfWeek == DayOfWeek.Saturday) textBox1.Text = "Weekend";

    }

为什么要使用数字?

于 2013-11-10T19:53:10.873 回答