For printed drafts of my papers, I would like to add a timestamp on every page with the date/time when the document was last compiled. Is there any LaTeX command that produces the current date/time at compile time? Even better, is there any package that already solves my problem?
3 Answers
One can use \today to get the current date, the command \currenttime from datetime package to get the current time, and fancyhdr package to add it to every page.
- 4,984
- 4
- 39
- 37
Expanding on Grigory's answer, here's a MWE. To get the date and time in a reasonable format, I found that I wanted the most recent version of datetime, which is dated 2010/02/06 (the version that is on my system - from TeXLive 2009 - is dated 2007). Using the default options for date and time format made the information too long (as it is, I decided it was better to shift the page number from the centre to the left).
\documentclass{article}
\usepackage{fancyhdr}
\usepackage[yyyymmdd,hhmmss]{datetime}
\pagestyle{fancy}
\rfoot{Compiled on \today\ at \currenttime}
\cfoot{}
\lfoot{Page \thepage}
\begin{document}
My great work.
\end{document}
If for some reason you can't use the 2010 version (or can't be sure that coworkers have it), a solution that works on the older datetime is as follows:
\documentclass{article}
\usepackage{fancyhdr}
\usepackage[us,12hr]{datetime} % `us' makes \today behave as usual in TeX/LaTeX
\fancypagestyle{plain}{
\fancyhf{}
\rfoot{Compiled on {\ddmmyyyydate\today} at \currenttime}
\lfoot{Page \thepage}
\renewcommand{\headrulewidth}{0pt}}
\pagestyle{plain}
\begin{document}
My great work.
\end{document}
This later version also keeps the pagestyle closer to the plain default, you can edit of course as you like. The date will also show as "ddmmyyyy" instead of "yyyymmdd" but that's probably fine for your needs.
(edited by Andrew in response to Juan's comment - I can't resist a challenge; and then edited again by Juan to improve the example)
- 62,139
- 32
- 140
- 169
- 153,724
- 43
- 389
- 751
-
Thanks for the answer and example. It's a shame, though, that the version of
datetimeon TeX Live is outdated, as I can't reliably expect collaborators to also have the latest version. I'll have to hack my way through the olderdatetime. – Juan A. Navarro Aug 04 '10 at 11:01 -
Thanks for the update! Hope you don't mind I cleaned up a bit your example for the older datetime so that it's less hackish. ;) – Juan A. Navarro Aug 04 '10 at 11:56
-
1@Juan A. Navarro: I guess I do have a bit of a tendency to go straight for the hack!
\makeatletteris a bit of a give-away. I'll add here (rather than editing yet again) that, for this case, the obvious advantages of the newer version ofdatetimeare: more formats and\currenttimeincludes seconds. – Andrew Stacey Aug 04 '10 at 12:21 -
There has been an update to the datetime package and it is now better to use: http://ctan.org/pkg/datetime2
-
-
4@Ruben It's a different package (
datetime2vsdatetime). Technically it's not an update but a replacement (or alternative). @Betsalel, it might be worth expanding your answer to include an example withdatetime2commands (e.g.\DTMnow,\DTMcurrenttime). – Nicola Talbot Jan 02 '17 at 18:55 -
@NicolaTalbot -- fair enough, but let's be frank. Does it add such novelty to say use the
datetime2macros instead of those fromdatetime? (pls: no offense @Betsalel, just trying to be honest.) In my opinion it's more a comment than an own answer. – Ruben Jan 03 '17 at 01:30 -
when I added this yesterday I didn't have enough points to comment. I agree that it was only comment worthy.
to note, when I started using the datetime package and looking the documentation up I was pointed to datetime2 which has more features.
– Betsalel Williamson Jan 03 '17 at 16:04 -
Shouldn't the
datetimepackage be overwritten with the new and improved code instead of adding a new package? This is like a non-commited upgrade e.g.python2andpython3. – Jonathan Komar Mar 01 '18 at 14:09
\currenttimewhich comes from a standard class? I'm not sure if people have installed that package. I'll share my file. – Sigur Nov 28 '13 at 00:06datetimeis obsolete, you should usedatetime2and the command for getting date and time becomes\DTMnow. – yannis Jan 29 '18 at 17:10