2

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)

Dabblernl
  • 15,831
  • 18
  • 96
  • 148
  • 1
    It is a false question to suppose it is "better" or "worse". –  Aug 28 '12 at 19:07
  • @pst Well, no bomb is better than do bomb ;-) When I debug the old VB6 code and set the locale for the computer the IDE is on to En-us I see that that the code translates "28-08-2012" to "8/28/12". So Vb6 uses a different means to find out which local settings to use. – Dabblernl Aug 28 '12 at 19:19
  • Yes, the keyword is "different". Perhaps related wrt how VB6 chooses regional settings http://stackoverflow.com/questions/1059930/a-better-cdate-for-vb6 –  Aug 28 '12 at 19:44
  • BTW If your code actually had `CDate("28-08-2012")` (i.e. if this was not a simplification of your real code), you should have used the date literal #8/28/2012# (which the VB IDE will convert to US date format for you if you enter #28/8/2012# IIRC). – Mark Hurd Aug 29 '12 at 02:16
  • @MarkHurd The code is a simplification. The actual code is `CDate(strSomeValue)` strSomeCValue being a strongly typed string – Dabblernl Aug 29 '12 at 07:35

1 Answers1

2

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.

rskar
  • 4,607
  • 25
  • 21