Jump to content
Server Maintenance This Week. ×

Calculating Age in Years, Months, Weeks, Days


This topic is 7341 days old. Please don't post here. Open a new topic instead.

Recommended Posts

  • Newbies

I have the following FMP7 calculation that displays an age as "xx Years, xx Months, xx Days". It works fine.

GetAsText ((Year (Get(CurrentDate)) - Year (DOB) - If (Get(CurrentDate) < Date (Month (DOB); Day (DOB); Year (Get(CurrentDate))); 1; 0)) & " Years, " &

GetAsText (Mod (Month (Get(CurrentDate)) - Month(DOB) + 12 - If (Day (Get(CurrentDate)) < Day (DOB); 1; 0); 12)) & " Months, " &

GetAsText (Day (Get(CurrentDate)) - Day (DOB) + If (Day (Get(CurrentDate)) >= Day (DOB); 0; If (Day (Get(CurrentDate) - Day (Get(CurrentDate))) < Day (DOB); Day (DOB); Day (Get(CurrentDate) - Day (Get(CurrentDate))))))) & " Days"

I'm hung up (as always) on weeks and would like the calculation to calculate and display as follows: "xx Years, xx Months, xx WEEKS, xx Days" where whole years, months, weeks are shown and any remaining days. I've fooled around with the date functions WeekOfYear and WeekOfYearFiscal without success (yet).

This is my first post. Be gentle. Your advice is most appreciated.

Version: v7.x

Platform: Mac OS X Panther

Link to comment
Share on other sites

X days = Int( X / 7 ) weeks and Mod( X, 7 ) days, so

GetAsText ((Year (Get(CurrentDate)) - Year (dob) - Case (Get(CurrentDate) < Date (Month (dob); Day (dob); Year (Get(CurrentDate))); 1; 0)) & " Years, " &

GetAsText (Mod (Month (Get(CurrentDate)) - Month(dob) + 12 - Case (Day (Get(CurrentDate)) < Day (dob); 1; 0); 12)) & " Months, " &

Let( days = GetAsText (Day (Get(CurrentDate)) - Day (dob) + Case (Day (Get(CurrentDate)) >= Day (dob); 0; Case (Day (Get(CurrentDate) - Day (Get(CurrentDate))) < Day (dob); Day (dob); Day (Get(CurrentDate) - Day (Get(CurrentDate)))))); Int( days / 7 ) & " Weeks, " & Mod( days; 7 ))) & " Days"

I used the Let function here so that I wouldn't have to duplicate the final hairy calculation, and instead defined it as 'days' to make it less complicated and more apparent.

Link to comment
Share on other sites

  • Newbies

Thanks so much! I noticed CASE statements replaced IF too. I still have to remember CASE is more "economical" than IF. LET is cool.

Now...to handle results of 1 so it'll display "1 Year" instead of "1 Years", etc.

Thanks again.

Link to comment
Share on other sites

Let(years = GetAsText ((Year (Get(CurrentDate)) - Year (dob) - (Get(CurrentDate) < Date (Month (dob); Day (dob); Year (Get(CurrentDate)))))); years & " Year" & Right("s, "; (years = 1)*-1 + 3)) &

Let(months = GetAsText (Mod (Month (Get(CurrentDate)) - Month(dob) + 12 - (Day (Get(CurrentDate)) < Day (dob)); 12)); months & " Month" & Right("s, "; months = 1)*-1 + 3)) &

Let(days = GetAsText (Day (Get(CurrentDate)) - Day (dob) + Case (Day (Get(CurrentDate)) >= Day (dob); 0; Case (Day (Get(CurrentDate) - Day (Get(CurrentDate))) < Day (dob); Day (dob); Day (Get(CurrentDate) - Day (Get(CurrentDate)))))); Int(days/7) & " Week" & Right("s, "; (Int(days/7) = 1)*-1 + 3) & Mod(days; 7) & " Day" & Left("s"; Mod(days; 7) <> 1))

I tweaked the original calc a little to remove redundant Case( ) statements and moved the ending parens to the end of the 'Years' portion, then used Let( ) again and some Right( ) functions with semi-boolean results to determine whether ", " or "s, " should be used.

Link to comment
Share on other sites

  • Newbies

Wow, thanks again!

I started fooling around with your earlier calc and had done LETS for years and months too. I was using CASE statements to handle the plurals though. Your new one's more elegant.

The last tweak is to make the display entirely dynamic, i.e., if any Years, Months, Weeks, or Days = 0 then display only those >= 1, e.g., 1 Year, 2 Weeks, 3 Days OR 1 Day OR 2 Years, 1 Week. Entering today's date, the field sh/be blank.

As a self-professed "calculation fiend", you're a great help. Looking over your code really helps me better understand all the functions (when I'm not thrashing on balancing parentheses...sigh). If I'm pressing my luck, just say so.

Thanks again.

Link to comment
Share on other sites

I think you may have overlooked a problem with the day calculation since all months are not created equal. As long as both dates are 28 or less you should have no problem.

Do some really careful checking of your results.

Someplace I have a set of calculations for older versions.

Link to comment
Share on other sites

This topic is 7341 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.