The Solution 4 given by
DaveyM69 using
DateTime.ParseExact
with
InvariantCulture
is very good.
I want to add that, it is better to use
d.M.yyyy
format instead of
dd.MM.yyyy
format as shown below
DateTime dateTime = DateTime.ParseExact("5.5.2012", "d.M.yyyy",
System.Globalization.CultureInfo.InvariantCulture);
because
dd.MM.yyyy
will work if exactly two digits are given for
date and month
like
05.05.2012
but will throw error if
one digit
is given for date and / or month like
5.5.2012.
The
d.M.yyyy
format will work both for
one digit and two digits
given for the date and month.
05.05.2012 and 5.5.2012
both will be read without any error.