Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i whant to compare date.now with date insert by client
using control validation in asp.net

What I have tried:

     <input id="input_date_travel_strat" runat="server" class="form-control form-control-sm" type="text" placeholder="Start trip" required=""/>            <asp:comparevalidator runat="server"   errormessage="The date must be greater than today"  controltovalidate="<%# Convert.ToDateTime(input_date_travel_strat)%>" Operator="GreaterThan" type="date"   valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

how can I compare date.now with date insert in input by client 
Posted
Updated 11-May-20 22:11pm
Comments
Richard MacCutchan 12-May-20 4:07am    
You are trying to compare a DateTime with a string. Use DateTime types for both values.

1 solution

 
Share this answer
 
Comments
Member 14626494 12-May-20 7:25am    
thanks for replay but I said date.now of the system
Member 14626494 12-May-20 7:31am    
<asp:button id="Button1" text="Submit" onclick="Button_Click" runat="server">

<asp:comparevalidator id="CompareValidator1" runat="server" errormessage="The date must be greater than today" forecolor="Red"
="" controltovalidate="input_date_travel_strat" operator="GreaterThan" type="Date" display="Dynamic">


------------behind code-------------


protected void Page_Load(object sender, EventArgs e)
{
CompareValidator1.ValueToCompare = DateTime.Today.ToShortDateString();
}
Maciej Los 12-May-20 7:49am    
You haven't read referenced article. There's one very important statement: "By default the ASP.Net CompareValidator does not work for dd/mm/yyyy format hence we will need to change the Culture property of the page to en-GB in the @Pagedirective of the ASP.Net Web Page"
You have to know that ToShortDateString() returns string in format which is depend on operating system regional settings.
Test this to see the difference:
CultureInfo ci = new CultureInfo("Pl-pl");
Thread.CurrentThread.CurrentCulture = ci;
string formatteddate = DateTime.Today.ToShortDateString();
Console.WriteLine(formatteddate);

ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
formatteddate = DateTime.Today.ToShortDateString();
Console.WriteLine(formatteddate);

Result:
2020-05-12
5/12/2020

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900