我有一个警报,在每月的特定日期弹出,但我需要警报跳过周末

时间:2017-12-13 22:55:09

标签: c# asp.net

如何才能让警报仅在周末跳过?我尝试使用DayOfWeek,但代码似乎没有阅读它。 详细的代码片段:(你可以忽略这个片段,它太详细和过时)

int staffId = Convert.ToInt32(ddlAssign.SelectedValue);
    int d1, d3, d5; 
    var staff = BLL.FindStaffByID(staffId);
    var alert = false;

    var deadline = DateTime.Today;
    var Day = DateTime.Now.DayOfWeek;
        if (txtDeadline.Text != "")
        {
            deadline = Convert.ToDateTime(txtDeadline.Text);
        }

    var bin = new ScheduleBin();
    bin.PackBins(staffId);
    var k1 = bin.PercentBusy(1);
    d1 = k1;
        if (d1 >= 100)
        {
            if (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(1))
            {
                if (Day == DayOfWeek.Saturday || Day == DayOfWeek.Sunday)
                {
                    alert = false;
                }
                else
                {
                    alert = true;
                }
            }
        }

    var k3 = bin.PercentBusy(3);
    d3 = k3;
        if (d3 >= 100)
        {
            if (deadline == DateTime.Today.AddDays(3))
            {
                if (Day == DayOfWeek.Saturday || Day == DayOfWeek.Sunday)
                {
                    alert = false;
                }
                else
                {
                    alert = true;
                }
            }
        }

    var k5 = bin.PercentBusy(5);
    d5 = k5;
        if (d5 >= 100)
        {
            if (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(7))
            {
                if (Day == DayOfWeek.Saturday || Day == DayOfWeek.Sunday)
                {
                    alert = false;
                }
                else
                {
                    alert = true;
                }
            }
        }

    if (alert)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "script", "<script language='javascript' type='text/javascript'>alert(\"Whoa there! Looks like " + staff.FirstName + " is a bit booked for that deadline (" + d1 + "% | " + d3 + "% | " + d5 + "%). Check out their task list and talk to the AE's to see about clearing out some room, or consider assigning the task to someone else. \");</script>", false);
    }  

有一半时间我尝试使用DayOfWeek我收到此错误:

An object reference is required for the non-static field, method, or property 'System.DateTime.DayOfWeek.get.

我想知道这个错误究竟意味着什么?

更短的代码摘要:

var alert = false;
var currentDay = DateTime.Today;

if (DateTime.Now.DayOfWeek.ToString() != "Saturday" || DateTime.Now.DayOfWeek.ToString() != "Sunday")
{
   if (currentDay == DateTime.Today || currentDay == DateTime.Today.AddDays(3))
   {
      alert = true;
   }
   else
   {
      alert = false;
   }
}

if (alert)
{
   ScriptManager.RegisterStartupScript(this, GetType(), "script", "<script language='javascript' type='text/javascript'>alert(\"ALERT MESSAGE\");</script>", false);
} 

对任何混淆道歉。 非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

我认为问题在于逻辑需要简化;可能有多种情况需要设置警报,而不仅仅是一种情况。另外,如果当周的当天是周末,请不要应用任何其他逻辑,因为它无关紧要。

int staffId = Convert.ToInt32(ddlAssign.SelectedValue);
var staff = BLL.FindStaffByID(staffId);
var alert = false;

var deadline = DateTime.Today;
var day = DateTime.Now.DayOfWeek;
if (!string.IsNullOrEmpty(txtDeadline.Text))
    deadline = Convert.ToDateTime(txtDeadline.Text);

/* Only perform logic if it's not a weekend */
if (day != DayOfWeek.Saturday && day != DayOfWeek.Sunday)
{       
    var bin = new ScheduleBin();
    bin.PackBins(staffId);

    /*Note inclusion of else statement to short circuit the logic */
    if (bin.PercentBusy(1) >= 100 && (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(1)))
    {
        alert = true;
    }
    else if (bin.PercentBusy(3) >= 100 && (deadline == DateTime.Today.AddDays(3)))
    {
        alert = true;
    }
    else if (bin.PercentBusy(5) >= 100 && (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(7)))
    {
        alert = true;
    }

    if (alert)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "script", "<script language='javascript' type='text/javascript'>alert(\"Whoa there! Looks like " + staff.FirstName + " is a bit booked for that deadline (" + d1 + "% | " + d3 + "% | " + d5 + "%). Check out their task list and talk to the AE's to see about clearing out some room, or consider assigning the task to someone else. \");</script>", false);
    } 
}

逻辑上,这可以简化为以下

var day = DateTime.Now.DayOfWeek;

/* Only perform logic if it's not a weekend */
if (day != DayOfWeek.Saturday && day != DayOfWeek.Sunday)
{
    var deadline = !string.IsNullOrEmpty(txtDeadline.Text)
        ? Convert.ToDateTime(txtDeadline.Text)  
        :DateTime.Today;

    var bin = new ScheduleBin();
    int staffId = Convert.ToInt32(ddlAssign.SelectedValue);
    bin.PackBins(staffId);

    var alert = (bin.PercentBusy(1) >= 100 && (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(1))
        || bin.PercentBusy(3) >= 100 && (deadline == DateTime.Today.AddDays(3))
        || bin.PercentBusy(5) >= 100 && (deadline == DateTime.Today || deadline == DateTime.Today.AddDays(7)));

    if (alert)
    {
        var staff = BLL.FindStaffByID(staffId);
        ScriptManager.RegisterStartupScript(this, GetType(), "script", "<script language='javascript' type='text/javascript'>alert(\"Whoa there! Looks like " + staff.FirstName + " is a bit booked for that deadline (" + d1 + "% | " + d3 + "% | " + d5 + "%). Check out their task list and talk to the AE's to see about clearing out some room, or consider assigning the task to someone else. \");</script>", false); 
    }
}

答案 1 :(得分:0)

我有类似的东西......尝试这样的事情。

 private void CheckTime(object sender, EventArgs e)
  {
     switch (DateTime.Now.DayOfWeek.ToString())
     {
        case "Saturday":
           {
              DoSomething();
              break;
           }
        case "Sunday":
           {
              DoNothing();
              break;
           }
     }
  }