# Take field, subtract date, to get age

I would like to have a computed field(?), that will take the value that is entered in a date time field and subtract that from the current date, to display in the computed field.  Is this possible or even the way notes is supposed to work?  What does the code look like?  Thanks!
LVL 6
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Sure,

Link to a couple of variations: http://www.experts-exchange.com/Applications/Email/Lotus_Notes_Domino/Q_21711859.html?query=difference+between+days&topics=133

The formula for the computed Field - basically subtracting dates leaves you with the difference in seconds, so you divide the difference by 24*60*60, also in order not to throw an error, you should check if your field date is not empty.

CurrentDate:=@Today;
ThisDate:=YourFieldName;
@if(thisDate !="";(CurrentDate - ThisDate) / 86400 + 1;"NA")
Groupware ConsultantCommented:
Marilyn, do you think the following is possible:
bd:= Birthday;

Nope, won't do it correctly either, because of the leap years.

Another one:
bd:= Birthday;
td:= @Today;
ndays:= (td - bd) / 86400;
nyears:= @Integer(ndays/365);

You get the drift...
Author Commented:
@Marilyng : how do i get that age to display in a view?  Do i need to not use a computed field?  Sorry... its like my 3rd day using Notes from a (limited) .Net background...
Commented:
:)).. where did birthday come from?  Got a snippet for that, too, somewhere... give me a moment to find it.
Commented:

The birthday formula - yy.mm, put a field for birthday on your form, computed for display, so it updates each time the form opens.

@Round(((@Year(@Today)-@Year(Birthday))*12 +(@Month(@Today)-@Month(Birthday)))/12;.2)

If you want to do this in views, well, you have three choices:
------------
Choice 1:  Set the view refresh indexes to at least once a day in the view properties,
Make sure you fill in your Birthday Field Name;
PUt this in the Column Formula:

TD:=@TextToTime("Today");
@Round(((@Year(TD)-@Year(Birthday))*12 +(@Month(TD)-@Month(Birthday)))/12;.2)
---------
CHOICE 2:
Leave the Formula:
@Round(((@Year(@Today)-@Year(Birthday))*12 +(@Month(@Today)-@Month(Birthday)))/12;.2)

Knowing that the twistee for the view will cause the server to refresh the view every second.

------
Choice 3:
Create an agent that will run every night and stamp all birthday form field: todayDate with today's date,
Create a field on your form: todayDate, computed, with the default formula = todayDate.
@Round(((@Year(todayDate)-@Year(Birthday))*12 +(@Month(todayDate)-@Month(Birthday)))/12;.2)

The agent, of course, has two lines in the formula:
@SetField("todayDate";@Today);
SELECT @All

Set a condition to run on your form, and schedule it to run on the server, shortly after midnight.  Make sure to save the agent with an ID that has permission to run agents on the server.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Groupware ConsultantCommented:
The form should have a field Birthday and then you follow Marilyn's post above, with one of the three options.

However, I think the formula to compute someone's age is not correct, you also need the day. The column formula:

bd:= Birthday;
td:= @Today;
bd1:= @Year(bd)*10000 + @Month(db)*100 + @Day(bd);
td1:= @Year(td)*10000 + @Month(td)*100 + @Day(td);
age:= @Integer((td1-db1)/10000);
age
Commented:
Really?  Why is it incorrect?  Most times, at least from HR perspective, they only want the month.

The correct posting would be to say, if you want a formula to the day, then use sjef's post.

If you need a formula to reflect the yy.mm then use Marilyn's post.

It would be correct to say a formula is incorrect if it didn't work,

But since we have a generic "age" specified, with no parameters, a formula to show year, or year +months, or Year + months + days would be correct, as long as the calculation specified is the result you get.

Groupware ConsultantCommented:
If I were born in 1966, on March 24, wouldn't your formula make me 40 already? And I've been searching all over, but I can't find my generic "age"... ;)
Commented:
I did say that the formula I posted would return yy.mm  or age + month:)  (HR likes to make eligible on the birthmonth, not the birthday)

LOL, you have typo's in your code: @Year(bd)*10000 + @Month(>>db<<)*100 + @Day(bd);@Integer((td1->>db1<<), when you paste yours into a field and try to run, you get error messages about expecting a number:))

However, here is another way that includes the DAY :) yuk,yuk.. and no typo's.
BD:=Birthday;
TD:=@Today;
@If(BD="";@Return("");"");
@Integer(@If(TD<@Date(@Year(TD);@Month(BD);@Day(BD));@Year(TD)-@Year(BD)-1;@Year(TD)-@Year(BD)))

Groupware ConsultantCommented:
bd, db, dbbd, bdbd, bbbbb *grunt*
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.