Link to home
Start Free TrialLog in
Avatar of edonasan
edonasan

asked on

DateDiff function

Hi,

I have a problem with the DateDiff function in VB6.

I compare with 2 dates to get the date difference. (dd-mm-yyyy)
eg. 1-12-2001 and 2-12-2001
*******************************************************

Scenario 1:

Date format in Regional Settings is dd-MM-yyyy.

Result = DateDiff("d", "1-12-2001", "2-12-2001")

and i get my result = 1, meaning 1 day.


Scenario 2:

Date format in Regional Settings is MM-dd-yyyy.

Result = DateDiff("d", "1-12-2001", "2-12-2001")

and i get my result = 30, meaning 30 days.

******************************************************

how can i get the format of the system date so as to format the suitable date format for the function to work correctly?

how shd i be independant of the Regional Settings date format to get the correct DateDiff result?

or any suggestion to resolve this problem?


Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Always use something that cannot be ambiguous:

Format(Date,"yyyy-mm-dd") or Format(Date,"dd mmm yyyy")

This will give 2001-12-01 or 01 Dec 2001 which cannot be confused. So take the dates and convert to either of these formats and then use this in the datediff. You should always then get the correct result.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS. To get values for DateSerial, use Split function if your DateString is always in dd-mm-yyyy format:
DateArr = Split(DateString,"-")
Date1 = DateSerial(DateArr(2),DateArr(1),DateArr(0))

Cheers
Avatar of drabaecus
drabaecus

TimCottee's suggestion works well, something like...

dayCount = datediff("d", format(date1, "dd/mm/yyyy"), format(date2, "dd/mm/yyyy"))


Avatar of edonasan

ASKER

hi Ark,

I have learnt something new on DateSerial function, thanks!
I have tried TimCottee's suggestion,
it didn't work when the regional settings for the short date is MM/dd/yyyy.

Thanks anyway :)

Thanks for points, glad I could help