How to calcualte date1 - date2 = # of years?

  • Thread starter Thread starter RH
  • Start date Start date
R

RH

Hi,

I would like to calculate the number of years between 2 dates. I tried
using the DateDiff function but I don't get the result that I want. MSDN
says:
If Interval is set to DateInterval.Year, the return value is calculated
purely from the year parts of Date1 and Date2.

I would like the function to consider all of the components of the date (not
just the year portion). I have thought about just getting the number of days
and dividing by 365 but I know I would have to account for leap years.

Any suggestions? Thanks in advance.
 
Decide what your rules are, then write a function. For instance, how many
years from 1/1/04 to 12/29/04? How about 2/29/04 to 2/28/05?

Does a partial year count at all?

Jeremy

Do you want the answer to be an integer, or would 1.67 be a possible answer?
 
Thanks. I will define my own rule.

Btw, is there a "standard" for this definition? Does Microsoft have a
standard for this?
 
Hi RH,

Have a look at the dateInteveral.subtract method and the
dateInterval.add.... methodes

It uses methods instead of properties and operators, therefore it looks a
little bit strange but it is easy to do.

As a very simple sample
\\\\
Dim span As TimeSpan = CDate("06/06/2003 12:00").Subtract(Now)
////

But there are a lot of posibilities

I hope this helps,

Cor
 
Back
Top