# Using \today in \hyperref package

Posting a question for the first time so please let me know if I missed anything.

I am working in a LaTeX document and here is what I want to achieve using the hyper ref package:

Have a URL that is linked to my website and has today's date embedded for referral tracking. For example, the URL should look like:

https://example.com?ref=text-2ndOct2020

Basically, adding "ref=text-date" allows me to track where the link was clicked from to get to my website. I want to use this for manual A/B testing of the content, the date in the referral helps me determine the version that did better.

Here is a MWE:

\documentclass[letter,oneside,10pt]{article}
\usepackage{hyperref}
\usepackage[ddmmyyyy]{datetime}

\begin{document}

\href{https://example.com?ref=text-date}{example.com}

\end{document}


This works, but what I'd ideally like to do is:

\href{https://example.com?ref=text-\today}{example.com}


so that the date can auto-update when I compile it for that particular day, the key being using \today to add today's date automatically to the link.

When I do use \today in \href in this particular solution, I get the following error:

Undefined control sequence. }
line 107: Undefined control sequence. }
line 107: Undefined control sequence. }
.
.
.
line 107: Undefined control sequence. }


I am using TeXStudio with a 2019 TeX-Live distribution, and compelling my document using XeLaTeX.

I am open to any solution but using \today is what came to my mind, I'm sure there are better ways to do this (if the \hyperref package supports it). Thanks!

• Welcome to TeX.SE. This is an excellent first question. Usually, hyperref should be one of the last packages loaded (with very few exceptions). But I doubt that is what is causing your problem. – Teepeemm Oct 2 '20 at 15:10

Here's what I tried:

\documentclass[letter,oneside,10pt]{article}
\usepackage{hyperref}
\usepackage[ddmmyyyy]{datetime}

\begin{document}

\href{https://example.com?ref=text-\today}{example.com}

\end{document}


Running this (I have a newer install), I don't get the errors that you're getting, but the URL comes out as:

https://example.com/?ref=text-%5Cprotect%20%5C@day%20=%5Cday%20%5Crelax%20%5C@month%20=%5Cmonth%20%5Crelax%20%5C@year%20=%5Cyear%20%5Crelax%2000/00/0


which is obviously not what we want.

The problem here is that the datetime package is doing all kinds of generalized things and expecting to produce text and not a token sequence.

So instead, let's go for a specialized but simplified version of what datetime does. Drop the \usepackage and instead do:

\renewcommand{\today}{\ifnum\day<10 0\fi\the\day \ifnum\month<10 0\fi\the\month\the\year}


which gives the desired URL.

• Wanted to add further to this already wonderful solution by Don Hosek. The current date format gives "DDMMYYY" –– what I was trying to achieve. I further amended the command to match "YYYY-MM-DD", so just leaving this solution out there for anyone else wanting to do a similar thing in the future. Here is the updated command: \newcommand{\trackdate}{\the\year-% \ifnum\month<10 0\fi\the\month-% \ifnum\day<10 0\fi\the\day} – Siddhant Khera Oct 2 '20 at 17:59