Question DateTime.ParseExact

jstranger

New member
Joined
May 10, 2010
Messages
2
Programming Experience
10+
Not certain that this is an internationalisation issue but seems possible hence posting here.

I am trying to convert various dates in string format into DateTime. The allowed formats are dd MMMM yyyy (e.g. 01 FEB 1950), MMMM yyyy (e.g. Feb 1950) and yyyy (e.g. 1950).

My code is:

Dim culture as New CultureInfo("en-GB")
Dim expectedFormats as String() = {"D", "dd MMMM yyyy", "MMMM yyyy", "yyyy"}
result = DateTime.ParseExact(anydate, expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces)

Here are some results as follows:

21 FEB 1950 - error
FEB 1950 - error
28 MAY 1950 - OK
MAY 1950 - OK
01 JUN 2006 - error
1950 - OK
08 FEB 1852 - error
25 FEB 1765 - error
27 OCT 1948 - error
05 SEP 1896 - error
OCT 1985 - error
13 APR 1791 - error
01 SEP 1827 - error
25 FEB 1833 - error
26 JUN 1993 - error

Has anyone any idea what I am doing wrong or can even spot any common factors to explain why some dates are OK and others are not?

Jon
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
Seeing as months with three letters in them (eg MAY) are working, try changing MMMM to MMM in your formats
 

jstranger

New member
Joined
May 10, 2010
Messages
2
Programming Experience
10+
That's rather embarrassing! I really should have spotted that May is the only month that is the same in MMM and MMMM formats!
 
Top Bottom