ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Compare Age between two Dates using C# and VB.Net in ASP.Net<p>Hi<a class="username" rel="iammann">iammann,</a></p> <p>Check this example. Now please take its reference and correct your code and restrict form submition.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">Date Of Borth:&lt;asp:TextBox ID=&#34;txtDOB&#34; runat=&#34;server&#34; Text=&#34;11/30/1993&#34; /&gt; &lt;br /&gt; Current Date:&lt;asp:TextBox ID=&#34;txtDate&#34; runat=&#34;server&#34; Text=&#34;11/30/2018&#34; /&gt; &lt;asp:Button Text=&#34;Sign Up&#34; runat=&#34;server&#34; OnClick=&#34;SignUp&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void SignUp(object sender, EventArgs e) { DateTime dob = Convert.ToDateTime(txtDOB.Text); DateTime dateToValidate = Convert.ToDateTime(txtDate.Text); TimeSpan ts = dateToValidate - dob; DateTime age = DateTime.MinValue + ts; int years = age.Year - 1; int months = age.Month - 1; int days = age.Day - 1; string totalAge = String.Format(&#34;{0} year/s {1} month/s {2} day/s old&#34;, years, months, days); if (years &gt;= 16 &amp;&amp; years &lt;= 25) { if (years == 25) { if (months == 0 &amp;&amp; days == 0) { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Success\\nYou are &#34; + totalAge + &#34;&#39;)&#34;, true); } else { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Failed\\nYou are &#34; + totalAge + &#34;&#39;)&#34;, true); } } else { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Success\\nYou are &#34; + totalAge + &#34;&#39;)&#34;, true); } } else { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Failed\\nYou are &#34; + totalAge + &#34;&#39;)&#34;, true); } }</pre> <p><strong><span style="text-decoration: underline;">VB.Net</span></strong></p> <pre class="brush: vb">Protected Sub SignUp(ByVal sender As Object, ByVal e As EventArgs) Dim dob As DateTime = Convert.ToDateTime(txtDOB.Text) Dim dateToValidate As DateTime = Convert.ToDateTime(txtDate.Text) Dim ts As TimeSpan = dateToValidate - dob Dim age As DateTime = DateTime.MinValue + ts Dim years As Integer = age.Year - 1 Dim months As Integer = age.Month - 1 Dim days As Integer = age.Day - 1 Dim totalAge As String = String.Format(&#34;{0} year/s {1} month/s {2} day/s old&#34;, years, months, days) If years &gt;= 16 AndAlso years &lt;= 25 Then If years = 25 Then If months = 0 AndAlso days = 0 Then ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Success\nYou are &#34; &amp; totalAge &amp; &#34;&#39;)&#34;, True) Else ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Failed\nYou are &#34; &amp; totalAge &amp; &#34;&#39;)&#34;, True) End If Else ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Success\nYou are &#34; &amp; totalAge &amp; &#34;&#39;)&#34;, True) End If Else ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;&#34;, &#34;alert(&#39;Validation Failed\nYou are &#34; &amp; totalAge &amp; &#34;&#39;)&#34;, True) End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/BpARUzp.gif" alt="" width="328" height="201" /></span></strong></p>https://www.aspforums.net:443/Threads/127973/Compare-Age-between-two-Dates-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/127973/Compare-Age-between-two-Dates-using-C-and-VBNet-in-ASPNet/Thu, 29 Nov 2018 01:49:01 GMT