Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
This could be done using the datenumber package. The number of days would be easy. If you want to get "You are YY years, MM month and DD days old" it is more tricky. See also this somehow related question. –  Martin Scharrer Feb 18 '11 at 16:52

2 Answers 2

up vote 15 down vote accepted

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!

share|improve this answer
2  
Most people call themselves 21 years old until their 22nd birthday. So \FPtrunc\myage{\myage}{0} might be more conventional. –  Matthew Leingang Feb 18 '11 at 18:10
1  
Simon & Garfunkel have a song that goes "I was 21 years when I wrote this song. I'm 22 now but I won't be for long. Time hurries on, and the leaves that are green turn to brown." –  Matthew Leingang Feb 18 '11 at 18:11
    
@Matthew Good point, but given that most people around here are mathematically inclined I will leave the choice up to them. –  Yiannis Lazarides Feb 18 '11 at 18:40
3  
Not to be too pedantic, but the average year has 365.2425 days in it, not 365.25—but I guess it doesn't matter unless you were born before 1900. –  Andrew Arnold Feb 18 '11 at 22:19
1  
@Hendrik I know what you mean. Where I grew I had to jump straight from 18.01 to 21 many a times! –  Yiannis Lazarides Feb 21 '11 at 17:24

The proposed solution does not seem to be completely accurate ; a perhaps more straightforward way of doing this is (replace DDMMYYYY by actual figures)

\usepackage{ifthen}
\newcounter{myage}
\setcounter{myage}{\the\year}
\addtocounter{myage}{-YYYY}
\ifthenelse{\the\month<MM}{\addtocounter{myage}{-1}}{}
\ifthenelse{\the\month=MM}{
  \ifthenelse{\the\day < DD}{\addtocounter{myage}{-1}}{}
}{}

then \themyage{} can be used to show your age.

share|improve this answer
    
Welcome to TeX.sx! You can highlight code by selecting it and pressing Ctrl + K. –  Torbjørn T. Jan 6 '12 at 16:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.