I talked about an issue I ran into between DateTime.MinValue and SqlDateTime.MinValue in a previous post. One other point I noticed while researching the topic was that, unlike almost all other types, SqlDateTime doesn’t have a TryParse method.
I really like the TryParse method because it helps me write code that is more terse and clean. I can easily use the TryParse methed as the condition for an if statement to handle the outcome without have to resort to ugly try catch blocks all over the place.
On Ian’s suggestion in the comments to my previous article I decided to take a stab at writing an extension method to implement this functionality. Now I said almost in the post title because I couldn’t get the TryParse method attached to the SqlDateTime native class. Now this could be due to this being my first attempt at an extension method so if anyone has any advice or suggestions on making this solution better feel free to leave them in the comments and I’ll update the code.
The solution
As I said I couldn’t get the method attached to the native type so this is the next best thing. I have declared a XSqlDateTime type that contains the TryParse method.
public static class XSqlDateTime {
public static bool TryParse(string str, out SqlDateTime output) {
try {
output = SqlDateTime.Parse(str);
return true;
}
catch {
output = SqlDateTime.MinValue;
return false;
}
}
}
This is easily used like so.
SqlDateTime date;
XSqlDateTime.TryParse("1/1/1953 12:00 AM", out date);
Just like all other implementations of TryParse on other types, if this method returns false it will assign the default type value to the output variable. In this case it is SqlDateTime.MinValue.
Hope this is useful to someone. I know it is for me. Cheers.
Did You Enjoy This Post?
Be sure to grab my RSS feed so you don't miss out on more great articles.
This Post Was Brought To You By
How do I save time? I use FreshBooks for invoicing.
Get Information Technology magazine subscriptions and white papers for FREE!

Did you like this post? Be sure to

April 2nd, 2008 at 1:51 pm
Make sure the class is static along with the method and that the calling class has a reference to the class with your extensions. This worked for me:
public static class Extensions
{
public static bool TryParse(this SqlDateTime dt, string input, out SqlDateTime output)
{
// logic here
return false;
}
}
April 2nd, 2008 at 5:01 pm
Hey, how about using the DateTime.TryParse:
public static class XSqlDateTime
{
public static bool TryParse(string str, out SqlDateTime output)
{
DateTime dt;
if (DateTime.TryParse(str,out dt))
{
if (dt SqlDateTime.MaxValue.Value)
{
output = SqlDateTime.MaxValue.Value;
}
else
{
output = (SqlDateTime)dt;
}
return true;
}
else
{
output = SqlDateTime.MinValue;
return false;
}
}
and saving the heavy block of the try & catch.
April 2nd, 2008 at 5:10 pm
Sorry, all the code mixed up.
You can see the code here:
http://mironabramson.com/blog/page/SqlDateTimeTryParse.aspx
April 3rd, 2008 at 2:07 am
Thanks John,
I’ll give your suggestion a try and then update the post.