Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert.ToDateTime vs System.DateTime.Parse

52 views
Skip to first unread message

tshad

unread,
Apr 20, 2006, 3:11:07 PM4/20/06
to
Is there any difference between convert.ToDateTime and
System.DateTime.Parse?

I am using them both and they seem the same.

Is one better to use than another?

Thanks,

Tom


Jon Skeet [C# MVP]

unread,
Apr 20, 2006, 3:22:37 PM4/20/06
to

Convert.ToDateTime uses DateTime.Parse internally, with the current
culture - unless you pass it null, in which case it returns
DateTime.MinValue.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ignacio Machin ( .NET/ C# MVP )

unread,
Apr 20, 2006, 3:36:59 PM4/20/06
to
Hi,

I bet one use the other, yep, according to MSDN Convert.ToDatetime use
DateTime.Parse. At least the overload method that receive a string.

and it seems that is the only one being used , all the other overloads of
Convert.ToDateTime says it throw an exception


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"tshad" <tschei...@ftsolutions.com> wrote in message
news:ufN$u4KZGH...@TK2MSFTNGP05.phx.gbl...

Fabio

unread,
Apr 20, 2006, 4:08:39 PM4/20/06
to
"tshad" <tschei...@ftsolutions.com> ha scritto nel messaggio
news:ufN$u4KZGH...@TK2MSFTNGP05.phx.gbl...

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;
}
For details give it a look with Reflector.

--

Free .Net Reporting Tool - http://www.neodatatype.net


tshad

unread,
Apr 20, 2006, 4:58:44 PM4/20/06
to
That what I wanted.

I can use either - just flip a coin :)

Thanks,

Tom
"Fabio" <znt....@virgilio.it> wrote in message
news:elm$yYLZGH...@TK2MSFTNGP04.phx.gbl...

0 new messages