Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 33907
  • Last Modified:

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.
0
mrichmon
Asked:
mrichmon
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now