循环一周的日期,直到在该周内找到假期

时间:2013-08-13 14:07:43

标签: c# asp.net

我有一个要求,在时间表中,我需要在假期落在一周时禁用字段,我需要将该日的字段禁用。但它的循环直到数据库中存在的假期结束。例如,我将采取startdate作为11.08.2013,我需要循环到本周的15.08.2013,因为8月15日是印度的独立日。

8月15日,该场地将被禁用。但在这里,我获得了像圣诞节一样的价值,这是今年的最后一个公众假期,并且它一直受到影响......

代码:

DateTime dtstart = Convert.ToDateTime(startdate);
con.Open();
OleDbCommand cmd1 = new OleDbCommand("select * from ind_holidays", con);
OleDbDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
    string holiday = dr1[0].ToString();
    string date = dr1[1].ToString().Substring(0, 10);
    DateTime holi_date = Convert.ToDateTime(date);
    while (dtstart <= holi_date)
    {
        if (dtstart.DayOfWeek == DayOfWeek.Monday)
        { 
                devmontxt.Text = holiday;
                devmontxt.Enabled = false;
                trmontxt.Text =  holiday;
                trmontxt.Enabled = false;
                lunmontxt.Text = holiday;
                lunmontxt.Enabled = false;
                permontxt.Text = holiday;
                permontxt.Enabled = false;
                totmontxt.Text = holiday;
                totmontxt.Enabled = false;

        }
        if (dtstart.DayOfWeek == DayOfWeek.Tuesday)
        { 

                devtuetxt.Text = holiday;
                devtuetxt.Enabled = false;
                trtuetxt.Text =  holiday;
                trtuetxt.Enabled = false;
                luntuetxt.Text =  holiday;
                luntuetxt.Enabled = false;
                pertuetxt.Text =  holiday;
                pertuetxt.Enabled = false;
                tottuetxt.Text = holiday;
                tottuetxt.Enabled = false;
        }
        if (dtstart.DayOfWeek == DayOfWeek.Wednesday)
        { 

                devwedtxt.Text =  holiday;
                devwedtxt.Enabled = false;
                trwedtxt.Text = holiday;
                trwedtxt.Enabled = false;
                lunwedtxt.Text =  holiday;
                lunwedtxt.Enabled = false;
                perwedtxt.Text =  holiday;
                perwedtxt.Enabled = false;
                totwedtxt.Text =  holiday;
                totwedtxt.Enabled = false;   
        }
        if (dtstart.DayOfWeek == DayOfWeek.Thursday)
        {
            devthutxt.Text = holiday;
            devthutxt.Enabled = false;
            trthutxt.Text = holiday;
            trthutxt.Enabled = false;
            lunthutxt.Text = holiday;
            lunthutxt.Enabled = false;
            perthutxt.Text = holiday;
            perthutxt.Enabled = false;
            totthutxt.Text = holiday;
            totthutxt.Enabled = false;

        }

        if (dtstart.DayOfWeek == DayOfWeek.Friday)
        {
            devfritxt.Text = holiday;
            devfritxt.Enabled = false;
            trfritxt.Text = holiday;
            trfritxt.Enabled = false;
            lunfritxt.Text = holiday;
            lunfritxt.Enabled = false;
            perfritxt.Text = holiday;
            perfritxt.Enabled = false;
            totfritxt.Text = holiday;
            totfritxt.Enabled = false;
        }
        if (dtstart.DayOfWeek == DayOfWeek.Saturday)
        {
                devsattxt.Text =  holiday;
                devsattxt.Enabled = false;
                trsattxt.Text =  holiday;
                trsattxt.Enabled = false;
                lunsattxt.Text =  holiday;
                lunsattxt.Enabled = false;
                persattxt.Text = holiday;
                persattxt.Enabled = false;
                totsattxt.Text =  holiday;
                totsattxt.Enabled = false;    

        }
        dtstart = dtstart.AddDays(1);
        continue;
    }
}
dr1.Close();
con.Close();

}

2 个答案:

答案 0 :(得分:1)

您需要找到一周的最后一天。

lastDay = dtstart.AddDays(7);

现在,由于您同时拥有开始日和结束日,因此您只能查询该周的假期。

select * from ind_holidays where holiday_day between @dtstart and @lastDay order by holiday_day

查询返回的第一个元素将是本周的第一个假日。

另一种选择是获取所有假期并将它们存储在列表中。然后你只需要循环列表中的项目。

答案 1 :(得分:0)

将您的假期保持在列表中并从现在开始仅循环一周,并检查列表中是否包含循环中的任何值以禁用它。 使用当前日期时间并添加所需的天数。