Search code examples
datetimevb6locale.net

How does CDate in VB6 differ from the VB.NET version?


We recently migrated some code from VB6 to Net 4. Among the code was this line:

If Now<CDate("28-08-2012") Then

One of our customers contacted support because of a program error and it surfaced that his computer produced an error on this line because he had his locale set to English (US). The exact same code did not bomb while it was VB6.

So, how is this difference to be explained?

(I am just finishing tearing out all the CDate functions from the Net code and replacing it by a yyyy,mm,dd DateTime constructor)


Solution

  • I think the issue here is (sadly) an over-zealous parsing routine for CDate in VB6. With the locale set to English (US), VB6 (and VBA for that matter) will return the same date for these two expressions:

    CDate("12-13-2000")
    CDate("13-12-2000")
    

    They both return #12/13/2000#. Clearly if the first number cannot be translated into a month (e.g. 13), it is then assumed to be a day of the month (a very bad assumption).

    So there it is.