我遇到了一个11年前的查询“自动计算年龄“关于如何自动计算一个人的年龄。
就像是:
I'm \myage{day}{month}{year} years old.
另一个我更感兴趣的例子是:
More than **XY** years have passed since the publication of the
Treatise on Electricity and Magnetism of 1873 (\DTMdisplaydate)...
在哪里XY是 1873 年和日期(\today fromdatetime2) tex 源文件编译。
该宏\DTMsaveddatediff
计算两个保存日期之间的差值(以天为单位),并将结果存储在给定的计数寄存器中。因此,也许可以通过用 365 天的常数除以年份来获得年份。
这些表达式如何在LuaLatex引擎内置的Lua脚本语言中实现?
答案1
这是我为您提供的答案,它将产生以下内容:
\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}
我只能说它并不完美,但如果你运行它几次,它确实会产生示例,也许它可以有所帮助。
答案2
公式是:
(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
除法的小数部分被丢弃。
平均能量损失
\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}
我保留了工作,因此您可以仔细检查编码是否正确。lua 手册位于:Lua 手册