Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using a small piece of code to validate Date Format in my Project and Getting this Error as
String was not recognized as a valid DateTime.

C#
private bool IsValidDate(string date)
       {
           if (date.Equals(string.Empty))
               return false;
           try
           {
               DateTime.Parse(date);
               return true;
           }
           catch
           { return false; }
       }


What I am passing is a String,e.g.("29/09/2010 11:02:50"), which inturns trying to Parse and where this error is coming.
Need help..

Regards
honeyashu
Posted

Hi You ahve to define which date format is required for Ex : DDMMMYYYY or
YYYY MM DD and so on then try use the code below

DateTime.ParseExact(myString,yyyy-MM-dd,  System.Globalization.CultureInfo.InvariantCulture);


replace YYY-MM-DD with the date format required
 
Share this answer
 
Comments
honeyashu 29-Sep-10 2:21am    
Tried already:

DateTime.ParseExact(date,"M/dd/yyyy",System.Globalization.CultureInfo.InvariantCulture);

but getting the same error.
Hey do this

DateTime.ParseExact(dateString,"dd/MM/yyyy",  System.Globalization.CultureInfo.InvariantCulture);
 
Share this answer
 
Comments
honeyashu 29-Sep-10 3:41am    
:) Thanx buddy but tried all those formats as well, problem seems to me is when i am getting string as Data + Time..., but this is what i am getting and needed to convert.

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