Custom Date Format Parsing in .Net Framework

By Akbar

One has to often import and export dates in different format in the day to day programming. Though most of the .Net developers know how to export (convert the date to string) in a specific format, it’s little tricky when it comes to importing that i.e. loading the date in a custom string format to the .Net DateTime object.

Fortunately, in the .Net there is just a one line code for parsing and loading the date and time to DateTime object too. Here is C# sample on how to do this:

1
2
3
4
5
string strDateTime = "2004-02-12"; // You might load this from the database
DateTime dateTime;                 // This will hold the target parsed value
 
DateTime.TryParseExact(strDateTime, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out item._listingEndDateTime);

I must admit that such utility functions really help to concentrate more on the actual problem. My kind regards to the developer of above method.

Tags: , ,