我有一个警报,该警报会在一个月的特定日子中弹出,但是我需要在周末跳过警报

  • 本文关键字:日子 在周末 有一个 一个 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);
} 

对任何混乱表示歉意。非常感谢任何帮助,谢谢!

我认为问题是需要简化逻辑;可能有多种案例设置警报,而不仅仅是一个。此外,如果本周的当天是一个周末,请不要使用其他任何逻辑,因为这没关系。

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

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

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

最新更新