简体   繁体   English

想要在开始日期文本框中添加一年的日期(减去一天,然后在结束日期文本框中返回)

[英]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.

相关问题 在文本框开始日期中自动输入日期时,在其他文本框中显示下一年的日期 - when enter a date in text box start date automatically Show dates of next one year in other textbox endtextbox 开始日期和结束日期应相差一年 - start and end date should be different for one year 如何使用javascript从日期增加或减去一天 - how add or minus one day from date using javascript javascript创建新日期减去一天 - javascript creates new date minus one day 使用日期选择器选择日期然后显示日期减去一天 - Using Datepicker to Select Date then Display Date Minus One Day 如何从另一个文本框当前文本框中填写+一年日期 - How to fill + one year date from another textbox current text box 使用 JavaScript(WordPress、Timber/Twig)将一天添加到“事件结束日期” - Add one day to 'Event End Date' with JavaScript (WordPress, Timber/Twig) 在 javascript 中添加一天 - Add one day to date in javascript boostrap datepicker允许选择结束日期作为开始日期的下一天 - boostrap datepicker allow to select end date as one day next to start date 当我从谷歌表格中的开始日期和结束日期创建事件时,日历中的结束日期比前一天 - When I create an event from a start date and end date in a google sheet, the end date in the calendar is one day earlier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM