Hi Friends
Today when i was Writing a Programe to upload .csv file to server in which i encountered a small problem that is the user was entering date in string format in .csv file as CCYYMMDD(20140130) format so when i was converting this string to date time format it was throwing exception so finally got the solution for the same.
here i wrote one method which will validate the data entered in string CCYYMMdd format and if it is valid then it is going to convert this string to date time format
string datefrmt = “20140130”; // this is correct format as it is in CCYYMMdd format
string datefrmt =”2014/01/30″; // this is not in correct format
if(ValidateDate(datefrmt)){
Console.Write(‘this is Valid date format’);
}
else
{
Console.Write(‘this is invalid date format);
}
public bool ValidateDate(string date)
{
try
{
System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
dtfi.ShortDatePattern = “yyyyMMdd”;
DateTime dt = DateTime.ParseExact(date, “d”, dtfi);
}
catch(Exception){
return false;
}
return true;
}