如何在ASP.net中创建节日日历

本文关键字:创建 日历 net ASP | 更新日期: 2023-09-27 18:08:36

我想使用asp.net制作节日日历,我使用了两个ajax日历和一个文本框,它是一个节日文本框,我们分别输入节日FromDate和ToDate。我想这样做点

  1. 如果我在文本框中输入Christmas并选择Fromdate=25/12/2011和ToDate=31/12/2011,那么它将有效

  2. 如果我选择fromDate=25/12/2011和ToDate=24/12/2011,那么它将无效

  3. 如果我选择Fromdate=25/12/2011和Todate=28/12/2011,那么它也是无效的,因为它在25/12/2011和31/12/2011之间

  4. 如果我选择fromdate=1/1/2011和ToDate=1/1/2011,那么它是有效的

  5. 如果我选择fromdate=21/12/2011和25/12/2011,那么它是无效的,因为已经在2011年1月1日过圣诞节了

所有的日期应该在gridview中显示,如25 dec-2011格式

下面是我的代码:
DateTime dt1 = Convert.ToDateTime(txt_fromdate.Text);
DateTime dt2 = Convert.ToDateTime(txt_todate.Text);
if (dt1 > dt2)
{
    con.Open();
    com = new SqlCommand("BTNN_MovieDB_Festival_Details_Insert", con);
    com.Parameters.Add("@fromdate", SqlDbType.VarChar).Value = dateformat_mmdd(txt_fromdate.Text.ToString().Trim());
    com.Parameters.Add("@todate", SqlDbType.VarChar).Value = dateformat_mmdd(txt_todate.Text.ToString().Trim());
    com.Parameters.Add("@return", SqlDbType.VarChar).Direction = ParameterDirection.ReturnValue;
    com.ExecuteNonQuery();
    con.Close();
    showdata();
}
else if (dt1 < dt2)
{
    lblerror.Text = "ToDate should be greater than FromDate";
}

如何在ASP.net中创建节日日历

这段代码应该起作用

     static void Main(string[] args)
    {
        // allFestivals holds all the festival list 
        // allFestivals values may come from some other data source like database
        List<Festival> allFestivals =new List<Festival>();
        // Simulate by inserting Christmas 
        Festival chirstmas = new Festival() 
        { 
            Name = "Christmas", 
            startDate = new DateTime(2011, 12, 25), 
            endDate = new DateTime(2011, 12, 31) 
        };
        AddFestival(allFestivals, chirstmas);
        // NewYear will not be inserted since 31-12-2011 is already 
        // marked as holiday by Christmas
        AddFestival(allFestivals, new Festival()
        {
            Name = "NewYear",
            startDate = new DateTime(2011, 12, 31),
            endDate = new DateTime(2012, 1, 01)
        });
        Console.ReadLine();
    }
    /// <summary>
    /// Add new festival to the list of festivals
    /// </summary>
    /// <param name="allFestivals"></param>
    /// <param name="newFestival"></param>
    static void AddFestival(List<Festival> allFestivals, Festival newFestival)
    {
        // If newFestival meets all the criteria only then add to the list
        if (ValidDates(newFestival) &&
            !NameExists(allFestivals, newFestival) &&
            !NonHoliday(allFestivals, newFestival) )
        {
            // allFestivals values may be strored into database
            allFestivals.Add(newFestival);
        }
    }
    /// <summary>
    /// Check if the newFestival startDate or endDate falls within any of the already
    /// existing festival start and end date
    /// </summary>
    /// <param name="dates"></param>
    /// <param name="newFestival"></param>
    /// <returns></returns>
    private static bool NonHoliday(List<Festival> dates, Festival newFestival)
    {
        return dates.Exists((date) => 
            newFestival.startDate >= date.startDate && newFestival.startDate <= date.endDate ||
            newFestival.endDate >= date.startDate && newFestival.endDate <= date.endDate);
    }
    /// <summary>
    /// If the festival name already exists, returns true else false
    /// </summary>
    /// <param name="dates"></param>
    /// <param name="newFestival"></param>
    /// <returns></returns>
    private static bool NameExists(List<Festival> dates, Festival newFestival)
    {
        return dates != null && 
            dates.Count() > 0  && 
            dates.FirstOrDefault((dt) => dt.Name == newFestival.Name) != null;
    }
    /// <summary>
    /// Validate if end date is greater than or equal to start date
    /// </summary>
    /// <param name="newFestival"></param>
    /// <returns></returns>
    private static bool ValidDates(Festival newFestival)
    {
        return newFestival.endDate >= newFestival.startDate;
    }
    // Data structure represenging festival details
    class Festival
    {
        public DateTime startDate { get; set; }
        public DateTime endDate { get; set; }
        public string Name { get; set; }
    }

这不是完整的语法,但应该可以帮助您了解需要做什么:

DateTime d1 = txtDate1.Text;
DateTime d2 = txtDate2.Text;
if (d1 < d2){
   //Invalid
}
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Table WHERE ([FromDate]<@Date1 AND [ToDate]>@Date1) OR (([FromDate]<@Date2 AND [ToDate]>@Date2)", conn);
//TODO: Add Parameters
if (((int)cmd.executescalar())>0){
    //Invalid
}