Rahul Patel Rahul Patel - 1 year ago 43
ASP.NET (C#) Question

In asp.net end date should not be more than 30 days of start date

i want to enter two dates in my textbox, and validate that end date is not older than 30 days of start date. and not before the start date.

<asp:TextBox ID="txtstrtdate" runat="server"></asp:TextBox> <asp:TextBox ID="txtenddate" runat="server"></asp:TextBox>
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnsubmit" runat="server" Text="Button" onclick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e)
DateTime firstday = Convert.ToDateTime(TextBox2.Text);
DateTime secondday = Convert.ToDateTime(TextBox3.Text);

Answer Source

you just need to use ticks function to chk difference. and compare that with expiry date.

DateTime d1 = DateTime.Now;
DateTime expiryDate = d1.AddDays(30);
DateTime startdate=  Convert.ToDateTime(txtstartdate.Text);
if (startdate.Ticks > d1.Ticks && startdate.Ticks <    expiryDate.Ticks)
         lblmsg.Text = "Congrats";
         lblmsg.Text = "Enter Valid Date";