DateDiff malfunction

vicks

New member
Joined
Dec 13, 2005
Messages
4
Programming Experience
1-3
DateDiff does not return me correct date difference in years :

Dim
dtDate1 AsDate
Dim DOB AsDate
DOB = #12/31/2000#
dtDate1 = #1/1/2005#

datediff(DateInterval.Year,dob,dtdate1) returns 5. It should return 4.
It must be using "Ceil" function internally.
What say?
 
Hi there,
I'm not too sure what your doing here. It should return 5 because it computes the time interval in years between 2000-2005, which is 5.

2000 -> 2001
2001 -> 2002
2002 -> 2003
2003 -> 2004
2004 -> 2005

Could you post back with exactly what your trying to accomplish here and we'll figure something out.
 
Date difference between 12/31/2000 and 1/1/2005 should be 4 years 1 day, right ?
12/31/2000 to 12/31/2004 = 4 Years
And 1/1/2005 = 1 Day

DateDiff() function doesn't look at the "date n month" part, it just looks at the "year" and calculates the difference...
 
Oh, sorry missed that. Well as far as i know because you have only specified the interval for a year, then that is all it will calculate.
As far as vb.net is concerned there is five years difference between the dates so that is what is being returned to you.
 
I'd suggest not using DateDiff at all. Use the DateTime and TimeSpan structures and their members. Date2.Subtract(Date1) returns a TimeSpan object. You can then use its Years or TotalYears property, depending on exactly what you want.
 
Back
Top