scrlttr2 document fails with datetime2 package

When using the datetime2 package in a scrlttr2 document, I get the error:

/usr/share/texlive/texmf-dist/tex/latex/datetime2/datetime2.sty:368: LaTeX Error: \today undefined.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ... l.368 \renewcommand*{\today} {%

The code to reproduce this looks like:

\documentclass[british]{scrlttr2}
%\usepackage{polyglossia}
%\setmainlanguage[variant=british]{english}
\usepackage{babel}
\usepackage{datetime2}
\begin{document}
\begin{letter}{test}
\opening{Hello}
\today
\closing{Bye}
\end{letter}
\end{document}


If you take out datetime2, the document works fine and \today is defined.

The only way I have found to temporarily fix the problem is to call \newcommand\today{} before loading datetime2 but it would be nice to know why I have to.

• Well the first guess is that scrlttr2 defines \today at \begin{document}. Feb 11, 2016 at 12:27
• @UlrikeFischer ah indeed, that seems to be the case. Although, looking at the source, there is also a definition for \today not in a \atBeginDocument{} but has some conditions attached to it which I don't really understand. Feb 11, 2016 at 12:41
• It's lucky you posted it now (and that I happened to see you question) as I was just about to upload a new version of datetime2. I've added a check for scrlttr2, and will upload the new version (which will be 1.4) shortly. Feb 11, 2016 at 12:48
• Oh brilliant! Thanks @NicolaTalbot. Perfect timing! Feb 11, 2016 at 12:51
• @NicolaTalbot looks like the same type of check would be needed for scrletter too since this is the new recommended way to create KOMA-letters… thanks a lot! May 27, 2020 at 5:55

As mentioned in the comments, the problem stems from scrlttr2 only defining \today at the start of the document (unlike most classes that define \today when the class loads). As from v1.4, datetime2 now checks for scrlttr2 (and doesn't assume \today is already defined). For earlier versions, the following is a temporary fix:

\documentclass[british]{scrlttr2}

\usepackage{babel}

\def\today{}
\usepackage{datetime2}
\let\orgtoday\today

\begin{document}
\let\today\orgtoday
\begin{letter}{test}
\opening{Hello}
\today
\closing{Bye}
\end{letter}
\end{document}