Custom Date Format Parsing in .Net Framework
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); |
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: Custom, DateTime, TryParse
This entry was posted
on Monday, June 2nd, 2008 at 4:12 pm and is filed under .Net Framework, C#.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Feedback & Comments
No Responses