Results 1 to 3 of 3

Thread: DateTime.ParseExact

  1. #1
    jstranger is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    May 2010
    Posts
    2
    Reputation
    0

    DateTime.ParseExact

    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

  2. #2
    InertiaM is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Posts
    663
    Reputation
    280
    Seeing as months with three letters in them (eg MAY) are working, try changing MMMM to MMM in your formats
    Always parameterize your queries - read more here

    "When people discover the center of the universe, a lot of them will be disappointed to find they are not it."

  3. #3
    jstranger is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    May 2010
    Posts
    2
    Reputation
    0
    That's rather embarrassing! I really should have spotted that May is the only month that is the same in MMM and MMMM formats!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •