Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check the condition if the date is correct format then some stuff else i want to be to show the error message.

ex:

C#
string date="2/28/2013 12:00:00 AM";


now i want to check the date has been correct format or not..
C#
If ( date=="mm/dd/yyyy ....")
   //inserting
else
   // message box to be displayed


can any body help me how to do this....
Posted
Comments
boogac 8-Feb-13 4:22am    
I just tried this simple code

string date = "28/02/2013";
DateTime time = Convert.ToDateTime(date);
MessageBox.Show ( String.Format("{0:dd MM yyyy}", Convert.ToDateTime(date)));

you can compare the date string with appopriate format sytle with String.Format() maybe it helps

Best practice to check in client side for validation.

You may use this code

XML
<asp:TextBox ID="tbxEventDate" Enabled="true" runat="server" CssClass="datepicker input-xlarge"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ControlToValidate="tbxEventDate" ID="rfvDate"
            ValidationGroup="validate" CssClass="text-error" Text="required" SetFocusOnError="true"></asp:RequiredFieldValidator>
 
Share this answer
 
Better to give validation on date entry textbox.
 
Share this answer
 
Comments
Naveen.Sanagasetti 8-Feb-13 4:06am    
that's not a textbox i fetch the information from Excel sheet that data i want to be to store it on SQL database before that i want to check the date has to be correct format or not

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