27

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.

Martin Scharrer
  • 262,582
Suar
  • 273
  • 3
  • 4

4 Answers4

23

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!

yannisl
  • 117,160
  • 5
    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
  • 2
    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. – yannisl Feb 18 '11 at 18:40
  • Yannis answer with Matthew's addition is exactly what I was looking for. Big thanks for help :) – Suar Feb 18 '11 at 19:06
  • 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 Feb 18 '11 at 22:19
  • @Yiannis: This is really not a matter of choice. If at all, it's a cultural question, but I don't believe any culture will round. If you're 20.9 years old, you may choose to say "I'll soon be 21 years old", but in the cultures I know of, "I'm 21 years old" would be a lie (in particular in the US if you want to buy alcohol). – Hendrik Vogt Feb 20 '11 at 08:23
  • 1
    @Hendrik I know what you mean. Where I grew I had to jump straight from 18.01 to 21 many a times! – yannisl Feb 21 '11 at 17:24
  • Need to beware of variable names too, Latex doesn't allow names like var1 with numbers in them! – Yan King Yin Jul 01 '15 at 14:21
8

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.

Torbjørn T.
  • 206,688
YoungFrog
  • 180
3

I have found that this works very nicely.

%
\catcode`@=11
\newcount\@tempnuma
\def\@null{null}
\def\age#1{\@age#1\@null}
\def\@age#1/#2/#3\@null{%
        \@tempnuma\year
        \advance\@tempnuma by -1900 % Comment this line out for dates of
                                    % the form 2/24/1967 - last 2 digits of yr.
        \advance\@tempnuma by -#3\relax  % gives # of years 
        %\number\@tempnuma
    \ifnum#1 > \month   %anniv month is later in year
            \advance\@tempnuma by -1\relax
    \else
            \ifnum#1 = \month   %anniv month is this month
                \ifnum#2 > \day %anniv day is later in month
                    \advance\@tempnuma by -1\relax
                \fi\relax
            \fi\relax
    \fi\relax
\number\@tempnuma}
\catcode`@=12
%

Usage is: \age{6/25/84} yields the correct age in years, which is what one usually wants. With some twiddling, one could get the months and days.

John Wooten
  • 131
  • 2
0
\usepackage{datenumber, fp}

\newcounter{birthday}
\setmydatenumber{birthday}{1995}{09}{06} % my birthday
\setdatetoday
\addtocounter{datenumber}{-\thebirthday} % today - birthday
\setdatebynumber{\thedatenumber} % get date
\FPsub\result{\thedateyear}{\thestartyear} % date year - start year (see datenumber lib doc)
\FPtrunc\myage{\result}{0} %truncate result

I am \myage years old.