[Tex/LaTex] Automated age calculation

calculationsconversiondatetime

How can I calculate my age at compile-time in LaTeX?

Something like:

I'm \myage{day}{month}{year} years old.

would be most awesome.

Best Answer

You can typeset your age easily using the datetime package and doing some simple calculations.

\documentclass{article}
\usepackage{datenumber,fp}
\begin{document}
\newcounter{dateone}%
\newcounter{datetwo}%

\setmydatenumber{dateone}{1990}{01}{01}%
\setmydatenumber{datetwo}{\the\year}{\the\month}{\the\day}%
\FPsub\result{\thedatetwo}{\thedateone}
\FPdiv\myage{\result}{365.2425}
\myage

\end{document}

You can truncate the figure correctly, i.e., if you 21.6 years old it will give you 21 (see edit), by changing the last line of the code as follows:

\FPround\myage{\myage}{0}\myage\ years old

See also this post.

Edit

Since the rounding of the age elicited a few comments I looked up the legal definition of age (which was probably appropriate in my example). Based on this I have changed the code from FPround to FPtrunc. Thanks to all that made comments. I also changed the days in the year to 365.2425 to increase the accuracy a bit for marginal cases!

Related Question