如何使用asp.net添加当前日期的天数

时间:2015-12-14 11:11:07

标签: javascript c# asp.net

我想将天数添加到当前日期,例如我想输入数字的天数,而不是我选择我想在当前日期添加的天数表示天,周和月,但是当我选择天数时或者它显示几天或几个月,但不能在当前日期添加它们 任何人都可以帮助我PLZ

 protected void TextBoxPredictDays_TextChanged(object sender, EventArgs e)
        {
            string selectPredictDays = DropDownList1.SelectedItem.Value;
            String Days1;
            int Result;
            Days1 = TextBoxPredictedClosing.ToString();
            if (selectPredictDays == "Days")
            {

                Result = Convert.ToInt32(TextBoxPredictDays.Text) * 1;
                TextBoxPredictedClosing.Text = Result.ToString();

            }
            else if (selectPredictDays == "Weeks")
            {

                Result = Convert.ToInt32(TextBoxPredictDays.Text) * 7;
                TextBoxPredictedClosing.Text = Result.ToString();
            }
            else if (selectPredictDays == "Months")
            {

                Result = Convert.ToInt32(TextBoxPredictDays.Text) * 30;
                TextBoxPredictedClosing.Text = Result.ToString();
            }

        }

这是我的设计代码

 <asp:TextBox ID="TextBoxPredictDays" runat="server" 
            ontextchanged="TextBoxPredictDays_TextChanged"  Width="101px" Height="14px" ></asp:TextBox>
      <asp:DropDownList ID="DropDownList1" runat="server" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>Days</asp:ListItem>
            <asp:ListItem>Weeks</asp:ListItem>
            <asp:ListItem>Months</asp:ListItem>
        </asp:DropDownList>

3 个答案:

答案 0 :(得分:6)

DateTime.Now.AddDays(int) - 天数
DateTime.Now.AddDays(int * 7) - 持续数周 DateTime.Now.AddMonths(int) - 数月

答案 1 :(得分:1)

DateTime dt = DateTime.Now.AddDays(int); Days
dt = DateTime.Now.AddMonths(int);  Months
dt = DateTime.Now.AddDays(int * 7); Week

答案 2 :(得分:1)

这是你的方法的样子:

protected void TextBoxPredictDays_TextChanged(object sender, EventArgs e)
        {
            string selectPredictDays = DropDownList1.SelectedItem.Value;
            if (selectPredictDays == "Days")
            {
                TextBoxPredictedClosing.Text = DateTime.Now.AddDays(Convert.ToInt32(TextBoxPredictDays.Text));
            }
            else if (selectPredictDays == "Weeks")
            {
                TextBoxPredictedClosing.Text = DateTime.Now.AddDays(Convert.ToInt32(TextBoxPredictDays.Text) * 7);
            }
            else if (selectPredictDays == "Months")
            {
                TextBoxPredictedClosing.Text = DateTime.Now.AddMonths(Convert.ToInt32(TextBoxPredictDays.Text));
            }
        }