DateTime.Parse vs Convert.ToDateTime

I am looking for documentation that recommends using one of DateTime.Parse vs Convert.ToDateTime and when each should be used / not used.

I am looking for info regarding efficiency and performance.

Please no opinions without supporting documenation, links, etc.

Thanks.
LVL 35
mrichmonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bruintjeCommented:
Hi mrichmon,

there is a good post about that here
source: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/34bf1ee24fa03de7?hl=en&
----------
This is Convert.ToDateTime(string)

public static DateTime ToDateTime(string value)
{
      if (value == null)
      {
            return new DateTime((long) 0);
      }
      return DateTime.Parse(value, CultureInfo.CurrentCulture);

}

And this is DateTime.Parse(string)

public static DateTime Parse(string s)
{
      return DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None);

}

That calls

internal static DateTime Parse(string s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
{
      DateTimeResult result1 = new DateTimeResult();
      result1.Init();
      if (!DateTimeParse.TryParse(s, dtfi, styles, ref result1))
      {
            throw DateTimeParse.GetDateTimeParseException(ref result1);
      }
      return result1.parsedDate;
}
----------

thus Convert.ToDateTime uses DateTime.Parse internally, with the current culture
if you pass it null, it will return DateTime.MinValue

if one or the other is better, think it does not matter much

hope this helps a bit
bruintje
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrichmonAuthor Commented:
Yes thank you.
0
AGBrownCommented:
There is no single answer for this. In the case of a string, Convert.ToDateTime simply returns 0 if the string is null, or calls DateTime.Parse() passing the string you passed to Convert.ToDateTime.

In the case of other types, the Convert.ToDateTime does:
    ((IConvertible)value).ToDateTime(null);
passing null as the formatprovider. I.e., the call is again going back to the original value's IConvertible.ToDateTime(IFormatProvider) method.

So, arguably, there's nothing in it; the COnvert class just passes the call off to another concrete implementation of a convert method, be that an IConvertible.ToDateTime implementation, or a Parse method.

If you want to find out specifics for a particular type, crack open the System assembly with ildasm and take a look into the Convert and the base type classes; or ask and I'll answer.

Andy
0
AGBrownCommented:
sorry, wrote that earlier, took a phone call, then posted it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.