i want to add the days to the current date for example i want to input the days in digits and than i select the days in which i want to add in current date means days, weeks and month but when i select the days or weeks or month it shows the days but can not add them in current date can any one help me 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();
}
}
here is my design code
<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>
Firstly, get the current date. Now, use AddDays() method to add days to the current date.
The DateTime. AddDays() method in C# is used to add the specified number of days to the value of this instance. This method returns a new DateTime.
AddDays() Method in C# This method is used to return a new DateTime that adds the specified number of days to the value of this instance. Syntax: public DateTime AddDays (double value);
DateTime.Now.AddDays(int)
- for days DateTime.Now.AddDays(int * 7)
- for weeks DateTime.Now.AddMonths(int)
- for months
DateTime dt = DateTime.Now.AddDays(int); Days
dt = DateTime.Now.AddMonths(int); Months
dt = DateTime.Now.AddDays(int * 7); Week
This is what your method would look like:
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));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With