Convert string CCYYMMDD to Date Time format using C#

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;
}

 

Advertisements

About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s