1

I came across an 11 year old query "Automated age calculation" about how to automate the calculation of a person's age.

Something like:

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

Another example in which I am more interested is:

More than **XY** years have passed since the publication of the
Treatise on Electricity and Magnetism of 1873 (\DTMdisplaydate)...

Where XY is year obtained as the difference between 1873 and the date (\today from datetime2) of tex source-file compilation .

The macro \DTMsaveddatediff computes the difference (in days) between two saved dates and stores the result in the given count register. So maybe the year can be obtained by its division with constant of 365 days.

How those expressions can be done in Lua scripting language built in LuaLatex engine?

JardaFait
  • 3,922
  • pgfcalendar package does that type of calculation, using the formula for Julian day number on Wikipedia. lua script can use the same formula. – Cicada Jul 27 '22 at 15:37
  • I can give you an answer if you want it the result gives you what you asked for but its possibly not the best. – Paul A Jul 27 '22 at 16:02

2 Answers2

3

enter image description here

This is the answer I have for you it will produce the following:

\documentclass{article}

\usepackage{datenumber}

\usepackage{datenumber, fp} \newcounter{birthday} \newcounter{today} \setmydatenumber{birthday}{1988}{02}{29} %insert birthday here \setmydatenumber{today}{\the\year}{\the\month}{\the\day}

\FPsub\result{\thetoday}{\thebirthday} \FPdiv\myage{\result}{365.2425} \FPround\myage{\myage}{0}

\newcounter{dateone} \newcounter{datetwo}

\newcommand{\difftoday}[3]{% \setmydatenumber{dateone}{\the\year}{\the\month}{\the\day}% \setmydatenumber{datetwo}{#1}{#2}{#3}% \addtocounter{datetwo}{-\thedateone}% \the\numexpr-\thedatetwo/365\relax\space year(s) %\the\numexpr(-\thedatetwo - (- \thedatetwo/365)*365)/30\relax\space month(s) %use if more accuracy is needed }

\begin{document}

Hello, I am \myage years old.\ \

More than \difftoday{1873}{0}{0} years have passed since the publication of the Treatise on Electrical and Magnetism of 1873 ...\% insert year here

\end{document}

I can only say it's not perfect but if you run it a few times it does produce the example, maybe it can help.

Paul A
  • 1,055
  • 4
  • 14
2

The formula is:

(1461 * (Y + 4800 + (M - 14)/12))/4 +(367 * (M - 2 - 12 * ((M - 14)/12)))/12 - (3 * ((Y + 4900 + (M - 14)/12)/100))/4 + D - 32075

with the fractional part of the divisions being discarded.

Julian day number

MWE

\documentclass{article}

\usepackage{luacode}

\begin{luacode}

JDN = function(Y, M, D) -- result = (1461 * (Y + 4800 + (M - 14)/12))/4 +(367 * (M - 2 - 12 * ((M - 14)/12)))/12 - (3 * ((Y + 4900 + (M - 14)/12)/100))/4 + D - 32075 -- tex.sprint("JDN=", result) -- tex.sprint("\par", math.floor(-2.5),", ",math.modf(-2.5)) -- i,f=math.modf(-2.5) -- tex.sprint("\par", i)

-- result = --(1461 * (Y + 4800 + (M - 14)/12)) xa=1461 xb=Y xc=4800 xd=(M - 14) xdi,xdf=math.modf(xd/12) --(1461 * (Y + 4800 + (M - 14)/12)) xe=(xa * (xb + xc + xdi)) xfi,xff=math.modf(xe/4) --/4 -- + --&& xg=367 xh= (M - 2 - 12 * xdi) xji,xjf=math.modf(xg * xh/12) --(367 * (M - 2 - 12 * ((M - 14)/12)))/12 -- - --&& xk=3 xl=(Y + 4900 + xdi) xmi,xmf=math.modf(xk * xl / 100) --(3 * ((Y + 4900 + (M - 14)/12)/100)) xni,xnf=math.modf(xmi/4) --/4 -- + --&& xo = D - 32075 result = xfi + xji - xni + xo -- tex.sprint("JDN=", result) return result end

yeardiff = function(jdn1,jdn2) resulti,resultf = math.modf((jdn2-jdn1)/365.2425) return resulti end

yeardifftoday = function(jdn1) jdn2=JDN(os.date("%Y"), os.date("%m"), os.date("%d")) resulti,resultf = math.modf((jdn2-jdn1)/365.2425) return resulti end

\end{luacode}

\newcommand\findjdn[3]{% \directlua{ tex.sprint(JDN(#1, #2, #3)) }}

\newcommand\findyears[6]{% \directlua{ tex.sprint(yeardiff(JDN(#1, #2, #3), JDN(#4, #5, #6))) }}

\newcommand\findyearstoday[3]{% \directlua{ tex.sprint(yeardifftoday(JDN(#1, #2, #3))) }}

\begin{document}

Result should be: 2000-01-01 (at noon) = 2451545

Calculation: \findjdn{2000}{1}{1}

I am \findyears{1988}{2}{29}{2022}{7}{28} years old. I was \findyears{1988}{2}{29}{1999}{2}{1} years old \findyearstoday{1999}{2}{1} years ago.

Over \findyears{1873}{12}{31}{2022}{7}{28} years ago, \ldots

Over \findyearstoday{1873}{12}{31} years ago, \ldots

\end{document}

I've left the working in, so you can double-check if the coding is correct. The lua manual is at: Lua Manual

Cicada
  • 10,129