Convert Date String to another Date string with different format
What you are doing is fine.
Probably you can improve it by using DateTime.TryParseExact
and on successful parsing, format the DateTime
object in other format.
string dateString = "20130916";
DateTime parsedDateTime;
string formattedDate;
if(DateTime.TryParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out parsedDateTime))
{
formattedDate = parsedDateTime.ToString("MM/dd/yyyy");
}
else
{
Console.WriteLine("Parsing failed");
}