The datetime2
package is designed to work with language packages, such as babel
and polyglossia
, but you need to additionally install the relevant language module. For example, datetime2-english
or datetime2-french
. The numeric style is the default, but you can use the package option useregional
to allow the language change to automatically change the date style.
Example:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\begin{document}
\selectlanguage{british}
\DTMdate{2016-01-30}.
\selectlanguage{french}
\DTMdate{2016-01-30}.
\end{document}
Produces:
Edit:
The date styles are designed to be expandable so commands like \today
can be used in contexts where information is written to an external file (such as table of contents or bookmarks). \DTMdate
is robust which means it can't be expanded but it doesn't need protecting (with \protect
). There's an expandable alternative \DTMdisplaydate
but it has a different syntax, which doesn't fit your requirement. These differences can be illustrated with the following examples:
\DTMdate
:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}
\pagestyle{headings}
\begin{document}
\tableofcontents
\newpage
\selectlanguage{british}
\section{\DTMdate{2016-01-30} example section}
\selectlanguage{french}
\section{\DTMdate{2016-01-30} example section}
\end{document}
The table of contents looks fine with this:
The page header can't convert the date to upper case (because it's robust) and it's also using the wrong language:
The PDF bookmarks can't process robust commands, so the bookmarks use the numeric form given in the argument of \DTMdate
:
Now modifying the example to use \today
:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}
\pagestyle{headings}
\begin{document}
\tableofcontents
\newpage
\selectlanguage{british}
\section{\today\ example section}
\selectlanguage{french}
\section{\today\ example section}
\end{document}
The contents page is the same as before, but the header on the next page is now correct (both language and case):
The PDF bookmarks are now fine:
The expandable version of \DTMdate{2016-01-30}
is \DTMdisplaydate{2016}{01}{30}{-1}
(or \DTMdisplaydate{2016}{01}{30}{5}
) and will work in the same way as \today
.
An alternative is to first store the date with \DTMsavedate
and later use it with \DTMusedate
, which is also expandable:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}
\DTMsavedate{mydate}{2016-01-30}
\pagestyle{headings}
\begin{document}
\tableofcontents
\newpage
\selectlanguage{british}
\section{\DTMusedate{mydate} example section}
\selectlanguage{french}
\section{\DTMusedate{mydate} example section}
\end{document}
This produces the same as the earlier example with \today
. The page headers and PDF bookmarks are correct.
datetime2
has\DTMdate
but it requires hyphens\DTMdate{2016-01-30}
. Is that okay or does it have to be without separators?\DTMdate{2016-01-30}
just prints "2016-01-30".