Home > database >  how to compare two time in asp .net
how to compare two time in asp .net

Time:09-30

I have two text boxes which contain time(like 12:26) that users inserted them.I want compare these two text boxes and the second one should always bigger than the first one. How should I write this code?

 <asp:TextBox ID="TextBox2" data-inputmask="'mask':['99:99']" runat="server" CssClass="txtAzSaat1" placeholder="-- : --" MaxLength="5" ></asp:TextBox>
            <asp:TextBox ID="TextBox3" data-inputmask="'mask':['99:99']" runat="server" CssClass="txtTaSaat1" placeholder="-- : --" MaxLength="5"></asp:TextBox><br />

CodePudding user response:

Check following way. You can modify as per your requirement.

         string TextBox2 = TextBox2.Text;
         string TextBox3 = TextBox3.Text;

         TimeSpan timeTextBox2;
         if (!TimeSpan.TryParse(TextBox2, out timeTextBox2))
         {
             // handle validation error
             return;
         }

         TimeSpan timeTextBox3;
         if (!TimeSpan.TryParse(TextBox3, out timeTextBox3))
         {
             // handle validation error
             return;
         }

         if (timeTextBox2 == timeTextBox3)
         {
             // Timespan matched
         }
         else
         {
             // Timespan not matched
         }
  • Related