The reason is that by default the
DateTimeFormat
of the
CurrentCulture
is used in parsing the date.
In your local machine the
DateTimeFormat
of the
CurrentCulture
may be in the
dd/MM/yyyy
format hence there may not be error.
Whereas on the webserver the
DateTimeFormat
of the
CurrentCulture
may be
MM/dd/yyyy
and since 30 is more than maximum month value the above error may be thrown.
So, when using
DateTime.ParseExact
the
InvariantCulture
can be used to avoid this problem as shown below:
DateTime date = DateTime.ParseExact("30/12/2012","d/M/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
The
d/M/yyyy
format matches the
date and month both in single digit and double digit
like
09/05/2012 and 9/5/2012
whereas the
dd/MM/yyyy
format requires both the date and month to be in exactly two digits. Otherwise it will throw error. So,
09/05/2012
can be read by it, but for
9/5/2012
it will throw error.
Hence, it is preferable to use
d/M/yyyy
format.