How can I check the date, not the future date in .net C #

Just wandering around how I can check the date, this is not a future date in .net C #.

Example:

I applied validation there for the necessary field validation. But for some reason, I have no idea how to apply the check to check the start date, to make sure that this is not a future date (the date is not greater than the current date)?

<tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> </td> </tr> 

than I wrote the following code to try out date validation. Check the date does not work for me: (

  <tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> <asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="activeDate" onservervalidate="valDateRange_ServerValidate" ErrorMessage="enter valid date" /> </td> </tr> " runat = "server"  <tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> <asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="activeDate" onservervalidate="valDateRange_ServerValidate" ErrorMessage="enter valid date" /> </td> </tr> 

code behind:

  protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args) { DateTime minDate = DateTime.Parse("1000/12/28"); DateTime maxDate = DateTime.Parse("2011/05/26"); DateTime dt; args.IsValid = (DateTime.TryParse(args.Value, out dt) && dt <= maxDate && dt >= minDate); } args)  protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args) { DateTime minDate = DateTime.Parse("1000/12/28"); DateTime maxDate = DateTime.Parse("2011/05/26"); DateTime dt; args.IsValid = (DateTime.TryParse(args.Value, out dt) && dt <= maxDate && dt >= minDate); } 
+4
source share
3 answers

DateTime implements the IComparer interface. Check if this value is greater than DateTime.Now

It makes no sense to disassemble it, just do:

 if(datetime1>datetime2) { .... } 
+20
source

I think you get the error message: DateTime.Parse("1000/12/28") Instead, try DateTime.MinValue . You can also use compareValidators ..

Also, any reason why you are not just fulfilling [your date] DateTime.now?

+3
source

Here is my code that does not check the date value in the future on the side :

ASPX Code:

 <tr> <td align="right"> Start Date: </td> <td> <asp:TextBox runat="server" ID="txtActiveDate" size="8"/>(YYYY-MM-DD) <!-- Validate if the entered date value is valid --> <asp:CompareValidator ID="cvIsActiveDateValid" runat="server" ControlToValidate="txtActiveDate" ErrorMessage="Invalid Start Date" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator> <!-- Validate if the entered value is not future dated --> <asp:CompareValidator ID="cvIsActiveDateNotInFuture" runat="server" ControlToValidate="txtActiveDate" ErrorMessage="Start Date cannot be a future date." Operator="LessThanEqual" Type="Date"></asp:CompareValidator> </td> </tr> 

ASPX.CS Code:

 protected void Page_Load(object sender, EventArgs e) { SetTodaysDateToCompareValidators(); ... } protected void SetTodaysDateToCompareValidators() { string defaultDateFormat = "YYYY-MM-DD"; string today = DateTime.Today.ToString(defaultDateFormat); cvActiveDate.ValueToCompare = today; } 
+1
source

All Articles