-1

例

  1. 月と年を選択
  2. ドロップダウン リストに月の週と日付を入力します

たとえば、4 番目の月 (4 月) を選択すると、WeekNo の出力は次のようになります。

Week No  Date
Week 1   [1-7]
week 2   [8-14]
Week 3   [15-21]
week.....
4

2 に答える 2

2

これは、それを行うための完全な C# コードです。

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadMonthDdl();
        }
    }

    private void LoadMonthDdl()
    {
        ddlMotnh.Items.Clear();
        ddlMotnh.Items.Add(new ListItem("January-"+DateTime.Now.Year,"1"));
        ddlMotnh.Items.Add(new ListItem("February-" + DateTime.Now.Year, "2"));
        ddlMotnh.Items.Add(new ListItem("March-" + DateTime.Now.Year, "3"));
        ddlMotnh.Items.Add(new ListItem("April-" + DateTime.Now.Year, "4"));
        ddlMotnh.Items.Add(new ListItem("May-" + DateTime.Now.Year, "5"));
        ddlMotnh.Items.Add(new ListItem("June-" + DateTime.Now.Year, "6"));
        ddlMotnh.Items.Add(new ListItem("July-" + DateTime.Now.Year, "7"));
        ddlMotnh.Items.Add(new ListItem("August-" + DateTime.Now.Year, "8"));
        ddlMotnh.Items.Add(new ListItem("Septeber-" + DateTime.Now.Year, "9"));
        ddlMotnh.Items.Add(new ListItem("October-" + DateTime.Now.Year, "10"));
        ddlMotnh.Items.Add(new ListItem("November-" + DateTime.Now.Year, "11"));
        ddlMotnh.Items.Add(new ListItem("December-" + DateTime.Now.Year, "12"));

    }
    protected void weekWithDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlweekWithDate.Items.Clear();
        int month = Convert.ToInt32(ddlMotnh.SelectedValue);
        DateTime beginDate= new DateTime(DateTime.Now.Year,month,1);
        int beginDay = beginDate.Date.Day;
        int noOfDay = System.DateTime.DaysInMonth(DateTime.Now.Year, month);
        int weekNo=1;
        int weekStartDate = 1;
        string str = "";
        while (beginDay<=noOfDay)
        {

            str = "week " + weekNo.ToString()+"["+weekStartDate;
            int newWeekN0 = GetWeekOfMonth(beginDate);
            if(weekNo==newWeekN0)
            {
            }
            else
            {
                str += "-" + beginDate.AddDays(-1).Day + "]";
                ddlweekWithDate.Items.Add(new ListItem(str, newWeekN0.ToString()));
                weekNo = newWeekN0;
                weekStartDate = beginDate.Date.Day;
            }
            beginDate = beginDate.AddDays(1);
            beginDay++;
        }
        if(!str.Contains("]"))
        {
            str += "-" + noOfDay + "]";
            ddlweekWithDate.Items.Add(new ListItem(str, weekNo.ToString()));
        }


    }
    public static int GetWeekOfMonth(DateTime date)
    {
        DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);

        while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
            date = date.AddDays(1);

        return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
    }

これはhtmlです

<asp:DropDownList runat="server" ID="ddlMotnh" AutoPostBack="True" onselectedindexchanged="weekWithDate_SelectedIndexChanged"/>
    <asp:DropDownList runat="server" ID="ddlweekWithDate" 
        />

ここではうまく機能します。あなたの目的のために、このC#コードをVBに変換するだけです

于 2013-07-09T05:57:40.633 に答える