C# datetime conversion with different formats using TryParseExact

Datetime conversion always seems too easy ,yet we find it frequently prone to bugs which might cost a lot sometimes. In a web application we restrict our users to select a date in a specific format which is great. Though integration projects are different where we get/pass XML/JSON data that needs to be processed at consumer’s end.

Also, useful when we are reading a log file which may contain different  date formats (This is where i got the bug :)). While reading these dates we  can use same function to convert date string to DateTime type for all valid date formats.

Problem Description: If your input date is a string in a certain format like “d/M/yy”, ” or “d/M/yy hh:mm:ss tt” etc and you expect output in a certain date format.

public static string GetFormattedDate(string inputDateString)
DateTime outputDate;
string outputDateFormat = "dd/MM/yyyy hh:mm:ss tt"; // can store in web.config
// array of possible supported date formats.
<strong>var allowedDateFormats = new[] { "d/M/yy", "d/M/yyyy", "dd/MM/yyyy", "d/M/yy hh:mm:ss tt", "d/M/yyyy   hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt" };</strong>

if( DateTime.<strong>TryParseExact</strong>(inputDateString ,<strong>allowedDateFormats</strong> , CultureInfo.InvariantCulture,
DateTimeStyles.None, out outputDate))
return (outputDate.ToString(outputDateFormat));
return "Not a valid date format.";

static void Main(string[] args)
     Console.WriteLine(GetFormattedDate(DateTime.Now.ToString("dd/MM/yyyy <strong>hh:mm:ss tt</strong>")); }

In the code above I am using TryParseExact method of DateTime class. This method has two overloads one of which accepts 2nd parameter as string (date format). This overload is what we use mostly. but other overloaded method accepts 2nd parameter as an array of string for valid date formats.
To read more about this method refer to msdn on http://msdn.microsoft.com/en-us/library/h9b85w22.aspx 

Mostly your input date format would be same always but here this GetFormattedDate function can be used to convert any number of date format with the same implementation.

It is a small piece of code  but very handy at times  🙂

I hope it is useful!


About saxenapraveen

I have 14 years of experience in enterprise software development,designing and leading team to provide reliable and scalable solutions for cloud and device ready businesses.

Posted on April 30, 2012, in .NET. Bookmark the permalink. 1 Comment.

  1. Yep..small piece of code but will be really useful in lot of scenarios

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: