DateTime.Parse vs Convert.ToDateTime

S-Soft 666 Reputation points
2023-11-18T14:40:01.9966667+00:00

Hello

As most of the times I cannot reply to my own threads, in continue of:

https://learn.microsoft.com/en-us/answers/questions/1429863/save-and-load-date-and-time

I need to get date and time from a custom date time control and save it into registry, then load it back to the control.

That's the best method to parse the string back to DateTime?

::

What about Convert.ToDateTime(now2) compared to DateTime.Parse?

Convert.ToDateTime seems to work too, but which one is better to avoid possible exceptions?

Thanks all

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,113 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,755 questions
{count} votes

Accepted answer
  1. P a u l 10,746 Reputation points
    2023-11-18T15:14:17.2833333+00:00

    There's not much difference, you can even see the difference if you decompile the source/lookup the C# reference source online.

    The main difference is that is coalesces a null input to a new DateTime(0) (i.e. DateTime.MinValue) which feels a bit wonky to me.

    User's image

    Apart from that the only other difference is that you can't pass DateTimeStyles like you can with DateTime.Parse / DateTime.TryParse so the resulting DateTime will always represent the date in Local time rather than Utc.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.