View Full Version : Datediff Problem


geno
02-21-2002, 02:56 PM
Hi, I have an employee table and need to convert the start date into years and months. This is the datediff function I have used:
Date in Years: DateDiff("yyyy",[date],Now()) & " years and " & DateDiff("m",[date],Now())-(DateDiff("yyyy",[date],Now())+Int(Format(Now(),"mmdd")<Format([date],"mmdd")))*12 & " months"
This works great for the dates that are over a year from the current date, but anything less returns the wrong date. eg: 11/14/01 returns 1 year and 3 months when it should be 3 months. Thanks for any help.

darcybaston
02-21-2002, 04:28 PM
Just change what you had before:

Date in Years: DateDiff("yyyy",[date],Now()) & " years and " & DateDiff("m",[date],Now())-(DateDiff("yyyy",[date],Now())+Int(Format(Now(),"mmdd")<Format([date],"mmdd")))*12 & " months"

Into

Date in Years: (DateDiff("yyyy",[date],Now())-1) & " years and " & DateDiff("m",[date],Now())-(DateDiff("yyyy",[date],Now())+Int(Format(Now(),"mmdd")<Format([date],"mmdd")))*12 & " months"

Just add a -1 to the first year thing, it seems to always be one year off right? Or maybe just test to see if the current year is one year away from the 'then' date using an IIf((year(now())-year(year([date])))=1, 0,DateDiff("yyyy",[date],Now()))

So if this year is one year away from the [date], then use a 0 as the year passed counter and if not, use what you had before.

regards,
darcybaston@mac.com

geno
02-21-2002, 04:33 PM
Thanks this did the trick...