I wrote a LuaLaTeX-only package named datestamp for this :)
The approach is pretty similar to the one in Slx64's answer, but this package has a few advantages.
- It doesn't overwrite anything. It checks if the key you have passed is present in the aux, and if yes it absolutely does nothing.
- It doesn't require any additional Lua modules.
- As it uses TeX commands for printing dates, it can print localized dates too.
A short example
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{datestamp}
\begin{document}
The date I write with \verb|\adddatestamp| doesn't change.
\adddatestamp{firstds}
\end{document}
Let's say your filename is random.tex. Running the above code will generate a random.ds file. Assuming you run it today; the .ds file will have the following text.
firstds = "December 3, 2021"
This is then interpreted as a lua-code which makes first a variable and then the value of this variable (which is the date) is printed in TeX. As long as you preserve the .ds file; your dates won't change, but let's say you lose your .ds file by mistake; there is still no need to worry. You can fool the program by writing your own .ds file and using exactly same keys in your document as argument of \adddatestamp command.
A localized example (Marathi)
\documentclass{article}
\usepackage{polyglossia}
\setdefaultlanguage{marathi}
\setmainfont[Script=Devanagari,Renderer=Harfbuzz]{Shobhika}
\usepackage{datestamp}
\begin{document}
\adddatestamp{firstdate}
\end{document}
An automated example
\documentclass{article}
\usepackage{kantlipsum}
\usepackage{datestamp}
\NewDocumentCommand{\dsfoot}{ }{%
\adddatestamp{pn\thepage}%
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\cfoot{}
\rfoot{Last modified: \dsfoot}
\begin{document}
\kant
\kant
\kant
\end{document}
If you add two more \kants on some other day, those pages will show that particular date. Your .ds file will look like the following:
pn1 = "December 3, 2021"
pn2 = "December 3, 2021"
pn3 = "December 3, 2021"
pn4 = "December 3, 2021"
pn5 = "December 3, 2021"
pn6 = "December 3, 2021"
pn7 = "December 3, 2021"
As explained earlier; if you want to have a different date on let's say page number 4, you can edit the .ds file; change the value of pn4 to, say, "December 22, 2021". From the next compile; output will show December 22, 2021 only on page 4.
Note: Please be careful while using the names of the variables. It seems like Lua doesn't like only numeric variables. I haven't dug deep; but using \thepage instead of pn\thepage throws a weird Lua error. So better have alphanumeric variables like ds1, pn1.