# How to automatically set CreationDate on PDF

When compiling a document using pdflatex one can set the PDF CreationDate of the document using \pdfinfo{...}. I would like to automatically set this to the date supplied to \date{...} in the document.

One way to do this is as follows.

\documentclass{article}
\usepackage[en-US]{datetime2}

\title{My Title}
\author{A. U. Thor}
\date{\DTMdisplaydate{2011}{12}{13}{-1}}
%\date{Winter 2011} % compiles, but results in blank CreationDate
%\date{\DTMdate{2011-12-13}} % compiles, but results in blank CreationDate due to robustness

\makeatletter
{\DTMsetstyle{pdf}
\pdfinfo{
/Title  (\@title)
/CreationDate (\@date 000000-05'00')
}}
\makeatother

\begin{document}
\maketitle % typesets date in a US-style, as expected
\end{document}


But there are a couple of things that don't seem ideal about this solution. First, I needed to write \@date 000000-05'00' in order to convert the date into a full time with a time-zone (the time-zone is needed in order avoid dates being defined in UTC and then being displayed as one day earlier in the US). Second, I needed to use \DTMdisplaydate instead \DTMdate in order for the macro to expand.

Since this seems like a fairly standard thing to do, I was wondering what solutions other pdflatex users have developed.

• The pgf calendar library has a number of formatting options. – John Kormylo Jul 12 '18 at 23:30

My personal approach would be to save the date like this:

\documentclass{article}
\usepackage[en-US]{datetime2}

\DTMsavetimestamp{creation}{2011-12-13T00:00:00-05:00}

\title{My Title}
\author{A. U. Thor}
\date{\DTMusedate{creation}}

\makeatletter
{\DTMsetstyle{pdf}
\pdfinfo{
/Title  (\@title)
/CreationDate (\DTMuse{creation})
}}
\makeatother

\begin{document}
\maketitle % typesets date in a US-style, as expected
\end{document}


You can provide a wrapper command like this:

\documentclass{article}
\usepackage[en-US]{datetime2}

\newcommand{\mydate}[1]{%
\DTMsavetimestamp{creation}{#1T00:00:00-05:00}%
\date{\DTMusedate{creation}}%
{\DTMsetstyle{pdf}%
\pdfinfo{
/CreationDate (\DTMuse{creation})
}}%
}

\title{My Title}
\author{A. U. Thor}
\mydate{2011-12-13}

\makeatletter
\pdfinfo{
/Title  (\@title)
}
\makeatother

\begin{document}
\maketitle % typesets date in a US-style, as expected
\end{document}


If you have a negative time zone, you'll need at least datetime2 version 1.5.3 (2018-07-20) to avoid a bug that accidentally inserted \relax before the time zone.

• When I compile either of the above (pdfTeX 3.14159265-2.6-1.40.18 (TeX Live 2017/W32TeX)) and inspect the properties from Adobe Acrobat Reader DC (version 2018.011.20040) the creation date is blank. Is this a problem with robustness? – banbh Jul 13 '18 at 17:17
• In case robustness was a problem I tried \DTMsetstyle{pdf}\edef\foobar{\DTMuse{creation}}\pdfinfo{/CreationDate (\foobar)} but AAR still shows "Created" as blank. – banbh Jul 13 '18 at 17:24
• I tried @nicola-talbot suggestions on an older TeXLive installation (pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016/W32TeX)) and it still resulted in a blank "Created:" field in AcroRd32.exe. – banbh Jul 16 '18 at 15:40
• @banbh pdfinfo, okular and evince all have the creation date set correctly with the above. I have TeX Live 2018 running on Fedora 27. With your \foobar example, try \show\foobar after you define it. That should show the fully expanded date in the transcript. – Nicola Talbot Jul 19 '18 at 10:51
• Ah! Thanks for the suggestion. There is a \relax in it; in fact \foobar is D:20111213000000\relax -05'00' and indeed \pdfinfo{/CreationDate (D:20111213000000\relax -05'00')} does not produce a date in AcroRd32.exe. Also, \edef\foobar{\DTMusedate{creation}000000-05'00'} followed by \pdfinfo{/CreationDate (\foobar)} works. I'm curious, why does \DTMuse insert a \relax before the time-zone? – banbh Jul 19 '18 at 15:49