• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • Last Modified:

Calculating Age from Two Dates

Very new, as in 2  weeks, to Filemaker and trying to clean up anothers mess. I need to perform a variety of date calculations, which all boil down to subtracting two dates from each other and reconstituting the difference back into years & months. Thus subtracting 1/1/2000 from 7/1/2006 would give me something on the order of "6 years 6 months" (or 6.6 which would be acceptable). I would not want the resulting answer to exceed two digits for years and 2 digits for months, so a resulting calculation of 10.11 (ten years eleven months) would be acceptable, but 10.111111111 (ten years ad nausem) would not.

Thanks,
Bob
0
Bob_Gardner
Asked:
Bob_Gardner
  • 5
  • 5
1 Solution
 
EdoshinCommented:
The main problem with date math are extreme samples. For example, how would you like to round the incomplete months? Could you please tell how the formula you're looking for must behave in the following cases?

March 1, 2006..March 20, 2006 = 0.1? or 0.0?

March 30, 2006..Apr 30, 2006 = 0.1? or 0.0?
0
 
Bob_GardnerAuthor Commented:
Edoshin
 Thanks for the response. Incomplete months would be 0.

  As for your specifc examples,

 March 1, 2006..March 20, 2006 = 0.1? or 0.0? -> Would be 0.0

 March 30, 2006..Apr 30, 2006 = 0.1? or 0.0? -> would most be 0.1, given 30 days in April and 31 in March.
0
 
EdoshinCommented:
I forgot yet another case: Apr 30...May 30. Should it be 0.0, because the end of March isn't reached yet or 0.1, because it seems natural. Anyway, here's a sketch of the formula:

Let( [
 date 1 = <your field>;
 date 2 = <your other field>;
 month 1 = Month( date 1 ); day 1 = Day( date 1 ); year 1 = Year( date 1 );
 month 2 = Month( date 2 ); day 2 = Day( date 2 ); year 2 = Year( date 2 );
 incomplete month =
   date 2 < If( Month( Date( month 2; day 1; year 2 ) ) = month 2;
    Date( month 2; day 1; year 2 );
    Date( month 2 + 1; 0; year 2 ) );
  number of months =
    ( year 2 - year 1 ) * 12 + ( month 2 - month 1 ) - Case( incomplete month; 1 ) ];

  Div( number of months; 12 ) & "." & Mod( number of months; 12 ) )

It calculates Apr 30..May 30 as "0.1"
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Bob_GardnerAuthor Commented:
Edoshin
 I think there is a problem brought on by the fact that I failed to specify that the FM version being used is 6.04. That said, unless there is a typo in your formula, there is no "Let" or "Div" functions. I have included my rendition of your formula, which I was going to test, but can not get FM to accept it in the calculation portion of the field definition.

Let([
date1=Today;
date2=Date of Birth;
month1=Month( date1); day1=Day(date1); year1=Year(date1);
month2=Month(date2); day2=Day(date2); year2=Year(date2);
incomplete month = date2< If(Month(Date(month2; day1; year2))=month2;
Date(month2; day1; year2);
Date(month2 + 1; 0; year2));
number of months=(year2-year1) * 12 + (month2-month1)-Case(incomplete month; 1)];
Div(number of months; 12) & "." & Mod(number of months; 12))
0
 
EdoshinCommented:
Then the formula will be way longer and hardly readable :)

Int( ( ( Year( Today) - Year( Date of Birth ) ) * 12
+ ( Month( Today) - Month( Date of Birth ) )
- ( Today< If( Month( Date( Month( Today), Day( Date of Birth ), Year( Today) ) ) = Month( Today),
  Date( Month( Today), Day( Date of Birth ), Year( Today) ),
  Date( Month( Today) + 1, 0, Year( Today) ) ) ) ) / 12 )
& "."
& Mod( ( ( Year( Today) - Year( Date of Birth ) ) * 12
+ ( Month( Today) - Month( Date of Birth ) )
- ( Today< If( Month( Date( Month( Today), Day( Date of Birth ), Year( Today) ) ) = Month( Today),
  Date( Month( Today), Day( Date of Birth ), Year( Today) ),
  Date( Month( Today) + 1, 0, Year( Today) ) ) ) ), 12 )

Also, I see you use the Today function, which is typically avoided by FileMaker developers and has been completely removed in v7. It can be used, but under quite specific circumstances and since you work with FileMaker for short time, you probably don't know its specifics. Would you like me to explain what's wrong with it?
0
 
Bob_GardnerAuthor Commented:
Edoshin ,

Thanks - I'll try you latest code in the morning - I'm in Seattle and its a little after midnight. By the way, after I posted that bit, and before I got your latest response, I read a short note on the 'today' problem, with the recommendation that Status(CurrentDate) be used. I am now using that - I hope that is correct. I understood that 'today' is very, very slow - amonst other issues. As an aside, I do NOT like FM (at least not what I have seen so far) and being a child at it is very frustrating. More later.
Bob
PS If this works out - I'm upping the points to 500. I sincerely appreciate your assistance.
0
 
Bob_GardnerAuthor Commented:
Edoshin,
Hmmm .. there seems to be a slight problem. I had the field define as a number, which resulted in 18.5.6, and then when I changed the field to text, it resulted in 18.4111 (not using the same set of dates in both tests). I am not sure what problem may be causing these results. Any ideas?
0
 
EdoshinCommented:
Something is wrong with the formula. Try this sample file: http://www.mytempdir.com/800622
0
 
Bob_GardnerAuthor Commented:
Edoshin,

 That worked! Great! Thanks lots.
Bob
0
 
EdoshinCommented:
You're welcome :) Maybe you'll eventually like FileMaker :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now