[Tex/LaTex] Redefinition of \today — Clash with babel

babel

I want my calendar dates to be formatted like DD.MM.YYYY

As I am from germany I use \usepackage[ngerman]{babel}. babel breaks my redefinition of \today to use leading zeros and I don´t know why it is not possible for me to redefine it. If babel is commented out, it works fine. But defining a new command which produces the desired format is possible, see the MWE below. Can someone explain why it is not possible to redefine \today? And is it safe to redefine it or might I break some things up that rely on \today so that it would be wiser to \todayx?

MWE

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\newcommand{\leadingzero}[1]{\ifnum #1<10 0\the#1\else\the#1\fi}
\renewcommand{\today}{\leadingzero{\day}.\leadingzero{\month}.\the\year}
\newcommand{\todayx}{\leadingzero{\day}.\leadingzero{\month}.\the\year}
\begin{document}
\today \\
\todayx
\end{document}

Best Answer

babel does definitions right at the start of \begin{document}, so even if \today is redefined after \usepackage{babel} this is not sufficient, but placing it in the \AtBeginDocument - hook will work. This does not require extra packages (although there nice packages such as datetime or datetime2!)

\documentclass{scrbook}
\usepackage[ngerman]{babel}

\newcommand{\leadingzero}[1]{\ifnum #1<10 0\the#1\else\the#1\fi}
\AtBeginDocument{%
\renewcommand{\today}{\leadingzero{\day}.\leadingzero{\month}.\the\year}
\newcommand{\todayx}{\leadingzero{\day}.\leadingzero{\month}.\the\year}
}
\begin{document}
\parindent=0em
\today 

\todayx
\end{document}

enter image description here

Related Question