About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.

Calculating Ages

This document requires the sample people.csv file.
\documentclass[captions=tableheading]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfkeys,pgfcalendar}
\usepackage{datatool}

\DTLloaddb{people}{people.csv}

\newcount\julianday
\newcount\juliantoday
\newcount\age

% Compute the Julian day number for today:
\pgfcalendardatetojulian{\year-\month-\day}{\juliantoday}%

\begin{document}
Ages as of \number\year-\number\month-\number\day\ are
listed in Table~\ref{tab:ages}.

\begin{table}[htbp]
\caption{Ages (as of \number\year-\number\month-\number\day)}
\label{tab:ages}
\centering
\begin{tabular}{lc}
\bfseries Name & \bfseries Age%
\DTLforeach*{people}%
 {\Forenames=forenames,\Surname=surname,\DoB=dob}%
 {%
    \\\Forenames\ \Surname &
    % Compute the Julian day number for the date of birth
    \pgfcalendardatetojulian{\DoB}{\julianday}%
    % Compute \age = (\juliantoday - \julianday)/365
    \age=\juliantoday
    \advance\age by -\julianday
    \divide\age by 365\relax
    \number\age
 }%
\end{tabular}
\end{table}

Non-ISO format:
\def\DoB{12/2/1995}%
\def\parsemdydate#1/#2/#3\endparsemdydate{#3-#1-#2}%
\pgfcalendardatetojulian{\expandafter\parsemdydate\DoB\endparsemdydate}{\julianday}%
% Compute \age = (\juliantoday - \julianday)/365
\age=\juliantoday
\advance\age by -\julianday
\divide\age by 365\relax
DoB = \DoB, age = \number\age.

\end{document}

Download calcage.tex or calcage.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs