3

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}
Enno
  • 606
  • Sorry, but how would you like the date to be formatted? As \todayx? – Alenanno Aug 23 '16 at 15:47
  • Yes exactly, \todayx gives me what I want but it would be nicer to use \today (in my opinion). – Enno Aug 23 '16 at 15:50
  • Use datetime or datetime2 package and your formatting issues will be solved, I think –  Aug 23 '16 at 15:50
  • @ChristianHupfer I was posting an answer with datetime, but is datetime2 supposed to be the new version? – Alenanno Aug 23 '16 at 15:53
  • @Alenanno: Yes, but I never used it so far... I don't know the real differences there –  Aug 23 '16 at 15:55

3 Answers3

3

You can load the datetime package with the ddmmyyyy option and \renewcommand{\dateseparator}{.} for the separator.

Output

enter image description here

Code

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage[ddmmyyyy]{datetime}
\renewcommand{\dateseparator}{.}

\setlength{\parindent}{0cm}

\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: \today

Todayx: \todayx
\end{document}
Alenanno
  • 37,338
3

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

  • Nice. So I guess it doesn't matter where you place the redefinition as long as babel is loaded? – Alenanno Aug 23 '16 at 15:58
  • @Alenanno: No, I don't think so, since babel's \AtBeginDocument stuff is placed later then, overwritting the O.P. versions of \today ... –  Aug 23 '16 at 15:59
  • Christian is right, simply moving my commands behind \begin{document} also solves the issue (but is not a good solution indeed ;-) ) – Enno Aug 23 '16 at 16:02
  • @Enno: That is possible too, of course, but for more general features, \AtBeginDocument (say in package or class code), is the better approach –  Aug 23 '16 at 16:40
  • @Christian: I definitely agree with you on that point. – Enno Aug 23 '16 at 16:48
1

Just for completeness, here's the datetime2 solution, but note that although it's using one of the base numeric styles it also requires datetime2-german (which is implicitly loaded) to prevent interference from babel.

\documentclass{scrbook}

\usepackage[ngerman]{babel}
\usepackage[style=ddmmyyyy,datesep=.]{datetime2}

\begin{document}
\today
\end{document}

Produces:

24.08.2016

Nicola Talbot
  • 41,153