Link to home
Create AccountLog in
Avatar of mwDev
mwDev

asked on

Problem with DateDiff and English (Canada) Regional Settings.

Hello Everyone,
I'm trying to get the age of a person in one of our legacy apps, but I'm not getting the same results between English (US) and English (Canada) regional settings. I'm using VB6 SP6 on Windows Server 2003 SP2.
Here's the sample code:
-------------------------------------
    Dim dtBirth As Date            'person's birth date
    Dim dtReport As Date      'report date
    dim lngDays as long            'number of days
    Dim lngYears as long      

    dtBirth = #12/4/1985#
    dtReport = #9/28/2007#

    'get number of days between dates
    lngDays = DateDiff("d", dtBirth , dtReport )
   
    'determine number of years
    lngYears = (lngDays / 365.25)
-------------------------------------
    With English US I'm getting the proper number, 21. With English Canada I'm getting 22.
Any help would be appreciated. Thanks
Avatar of MELeBlanc
MELeBlanc
Flag of United States of America image


I believe the reason is that in your example, the DOB is being interpreted as DD/MM/YYYY for English Canada... making them 22.

You'd have to account for this change in your code.

- M
I think this will give you the same answer in both cases:

lngYears = int(lngDays / 365.25))

Having said that, it's still  not 100% accurate.

 
Try this:
    Dim dtBirth As Date            'person's birth date
    Dim dtReport As Date      'report date
    Dim lngYears as int  

    dtBirth = #12/4/1985#
    dtReport = #9/28/2007#

    'get number of years between dates
    lngYears = DateDiff("d", dtBirth , dtReport )
   
    if DatePart("y",dtReport) < DatePart("y",dtBirth) then
      lngYears = lngYears - 1
   end if
Avatar of mwDev
mwDev

ASKER

I my previous posting to duplicate the problem with the English (Canada) settings the dates must be
'dd/mm/yyyy
dtBirth = #4/12/1985#
dtReport = #28/9/2007#

I believe the datediff is inverting the mongth and day when in Engilsh (Canada).
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
may be use fix?

lngYears = Fix(lngDays / 365.25)
Avatar of mwDev

ASKER

In theory the results should be the same regardless of regional settings. When I use English (Canada) I get 7968 days, however when I switch to English (US) I get 8204 days. Which is the different when the dates are inverted.
8204 is getting from  April 12, 1985, instead of Dec 4, 1985

You may want to extract the date and then use dateserial to compose the date before checking the diff