[英]want to add one year to date in start date text box wilh minus one day and return it in end date textbox
I want when I enter date in dd/mm/yyyy in start text box (TxtStartDate), it will automatically fill the end text box (TxtEndDate) by adding one year minus one day in above entered date for example if the user input is '15/04/2014' then the output will comes '14/04/2015' please provide your solution with javascript, jquery or asp.net c# 我想要当我在开始文本框(TxtStartDate)中以dd / mm / yyyy输入日期时,它将通过在输入的日期中加上一年减去一天来自动填充结束文本框(TxtEndDate),例如,如果用户输入为' 15/04/2014”,那么输出将为“ 14/04/2015”,请使用javascript,jquery或asp.net c#提供您的解决方案
my HTML Code: 我的HTML代码:
<tr>
<td></td>
<td style="text-align: right"><asp:TextBox ID="TxtEndDate" runat="server"
Width="177px" AutoPostBack="True" style="height: 22px"></asp:TextBox>
</td>
<td style="text-align: right">end date</td>
<td style="text-align: right">
<asp:TextBox ID="TextStartDate" runat="server" Width="177px"
AutoPostBack="True" style="height: 22px"></asp:TextBox></td>
<td style="text-align: right; direction: rtl;">
start date
</td>
</tr>
I have two textboxes in my asp.net c# code 我的asp.net C#代码中有两个文本框
For this task, you can take advantage of OnTextChanged
event of asp.net textbox controls. 对于此任务,您可以利用asp.net文本框控件的OnTextChanged
事件。 To automatically generate the code, you can directly double-click on the txt_Start
in the design mode. 要自动生成代码,您可以在设计模式下直接双击txt_Start
。 For this event to be triggered, you will need to add this AutoPostBack="True"
to txt_Start
Please see the following: 为了触发此事件,您需要将此AutoPostBack="True"
到txt_Start
请参见以下内容:
Start:<asp:TextBox ID="txt_Start" runat="server" AutoPostBack="True" OnTextChanged="txt_Start_TextChanged"></asp:TextBox>
<asp:Label ID="lbl_wrongdate" runat="server" Text=""></asp:Label>
<br />
End: <asp:TextBox ID="txt_End" runat="server"></asp:TextBox>
Then, the logic is very straightforward, I think it is self-explanatory: 然后,逻辑非常简单,我认为这是不言而喻的:
protected void txt_Start_TextChanged(object sender, EventArgs e)
{
DateTime start = new DateTime();
//using System.Globalization;
if (DateTime.TryParseExact(txt_Start.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out start))
{
DateTime end = start.AddYears(1).AddDays(-1);
txt_End.Text = end.ToString("dd/MM/yyyy");
lbl_wrongdate.Text = "";
}
else
{
lbl_wrongdate.Text = "Wrong date format. Allowed formats is dd/mm/yyyy";
txt_End.Text = "";
}
}
After you enter a valid date to txt_Start
and press Tab
, you will see txt_End
is automatically set. 输入txt_Start
的有效日期并按Tab
,您将看到txt_End
被自动设置。
in C#,its simple,can use date.AddYears()
and date.Adddays()
在C#中,它很简单,可以使用date.AddYears()
和date.Adddays()
first add 1 year by using 首先使用增加1年
Date dtedate = new Date()
dtedate=txtstartdate.Addyears(1)
then substract 1 day by 然后减去1天
dtedate1=dtedate.Adddays(-1)
then assign to the control 然后分配给控件
txtEnddate=dtedate1
hope this helps 希望这可以帮助
updates: 更新:
You can simplify the code to a single line.so it the code will be 您可以将代码简化为一行,因此代码将是
Date dtedate = new Date()
dtedate=datetime.parse(txtstartdate.text,"dd/MM/yyyy");
txtEnddate.text=convert.tostring(dtedate.AddYears(1).AddDays(-1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.