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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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),Date Arr(1),Dat eArr(0))
Cheers
DateArr = Split(DateString,"-")
Date1 = DateSerial(DateArr(2),Date
Cheers
TimCottee's suggestion works well, something like...
dayCount = datediff("d", format(date1, "dd/mm/yyyy"), format(date2, "dd/mm/yyyy"))
dayCount = datediff("d", format(date1, "dd/mm/yyyy"), format(date2, "dd/mm/yyyy"))
ASKER
hi Ark,
I have learnt something new on DateSerial function, thanks!
I have learnt something new on DateSerial function, thanks!
ASKER
I have tried TimCottee's suggestion,
it didn't work when the regional settings for the short date is MM/dd/yyyy.
Thanks anyway :)
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
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.